Moth.Tasks 1.0.0

dotnet add package Moth.Tasks --version 1.0.0                
NuGet\Install-Package Moth.Tasks -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Moth.Tasks" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Moth.Tasks --version 1.0.0                
#r "nuget: Moth.Tasks, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Moth.Tasks as a Cake Addin
#addin nuget:?package=Moth.Tasks&version=1.0.0

// Install Moth.Tasks as a Cake Tool
#tool nuget:?package=Moth.Tasks&version=1.0.0                

Moth.Tasks

Low-allocation async/synchronous task library

Provided as an alternative to the built-in System.Threading.Tasks system, for high-performance applications where excessive garbage collection is undesired.

Motive

When using the System.Threading.Tasks.Task type, a new object will be created to store data for each task enqueued. This is not a problem when a small number of tasks are enqueued, yet in high-performance applications, this extra pressure on the garbage collector can result in adverse performance.

The Moth.Tasks package can mitigate this problem, by using an internal buffer for task data, so as the only allocation is the task buffer itself.

How to Use

The TaskQueue type provides methods for enqueueing and executing tasks.

Tasks can be enqueued as a System.Action/System.Action<T1, ...> delegate:

// Create a new TaskQueue
TaskQueue tasks = new TaskQueue ();

// Enqueue a task taking two integer parameters: a & b, as an Action<int, int>
// 2 & 2 is supplied as arguments for both parameters
tasks.Enqueue (2, 2, (int a, int b) =>
{
    int result = a + b; // Add a & b together
    Console.WriteLine ($"The result of {a} + {b} is {result}"); // Print the result to the console
});

or as a struct implementing the ITask interface:

struct AdditionTask : ITask
{
    public int A, B; // Two numbers which will be added together
    
    // ITask.Run is invoked when the task is executed
    public void Run ()
    {
        int result = A + B; // Add a & b together
        Console.WriteLine ($"The result of {a} + {b} is {result}"); // Print the result to the console
    }
}

...

// Enqueue an AdditionTask with 2 as arguments for A & B
tasks.Enqueue (new AdditionTask { A = 2, B = 2 });

To execute the enqueued tasks, invoke either the TaskQueue.RunNextTask or TaskQueue.TryRunNextTask method:

if (tasks.TryRunNextTask ()) // Runs the next task in the TaskQueue and returns true, or returns false if the queue was empty
{
    ...
}

tasks.RunNextTask (); // Blocks until a task is enqueued, then runs it

To execute tasks in a background thread, the Worker & WorkerGroup can be used:

// A Worker represents a Thread which will continuously execute tasks in the background 
Worker worker = new Worker (new TaskQueue (), true, true);

// A WorkerGroup consists of multiple workers, all executing tasks from the same TaskQueue
WorkerGroup workerGroup = new WorkerGroup (4, new TaskQueue (), true, true);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 233 4/9/2022