DispatchSharp 1.5.0

dotnet add package DispatchSharp --version 1.5.0
NuGet\Install-Package DispatchSharp -Version 1.5.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="DispatchSharp" Version="1.5.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DispatchSharp --version 1.5.0
#r "nuget: DispatchSharp, 1.5.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 DispatchSharp as a Cake Addin
#addin nuget:?package=DispatchSharp&version=1.5.0

// Install DispatchSharp as a Cake Tool
#tool nuget:?package=DispatchSharp&version=1.5.0

DispatchSharp

https://www.nuget.org/packages/DispatchSharp/

A library to make multi-threaded dispatch code more testable.

Models a job dispatch pattern and provides both threaded and non threaded implementations.

Getting Started

Doing a batch of work:

void DoBatch(IEnumerable<object> workToDo) {
	var dispatcher = Dispatch<object>.CreateDefaultMultiThreaded("MyTask");

	dispatcher.AddConsumer(MyWorkMethod);
	dispatcher.Start();
	dispatcher.AddWork(workToDo);
	dispatcher.WaitForEmptyQueueAndStop();	// only call this if you're not filling the queue from elsewhere
}

or

Dispatch<int>.ProcessBatch("BatchRequests", Enumerable.Range(0, times).ToArray(), i => {
        . . .
    },
    threadCount,
    ex => {
        Console.WriteLine("Error: " + ex.Message);
    }
);

Handling long running incoming jobs:

dispatcher = Dispatch<object>.CreateDefaultMultiThreaded("MyService");
dispatcher.AddConsumer(MyWorkMethod);
dispatcher.Start();
.
.
.
dispatcher.AddWork(...);

Using a polling method to handle incoming jobs in a long-running process:

var dispatcher = Dispatch<object>.PollAndProcess("MyService", myPollingSource);
dispatcher.AddConsumer(MyWorkMethod);
dispatcher.Start();

with a method defined like

void MyWorkMethod(object obj)
{
	. . .
}

Multiple consumers

A dispatcher can be given multiple consumers. In this case, each consumer will get given each work item added.

So with

dispatcher = Dispatch<string>.CreateDefaultMultiThreaded("MyService");
dispatcher.AddConsumer(wi=> Console.Write($" Consumer 1, work item {wi};"));
dispatcher.AddConsumer(wi=> Console.Write($" Consumer 2, work item {wi};"));
dispatcher.AddConsumer(wi=> Console.Write($" Consumer 3, work item {wi};"));
dispatcher.Start();

dispatcher.AddWork("1");
Console.WriteLine();
dispatcher.AddWork("2");
Console.WriteLine();
dispatcher.AddWork("3");
Console.WriteLine();

We should get output similar to

 Consumer 1, work item 1; Consumer 2, work item 1; Consumer 3, work item 1;
 Consumer 1, work item 2; Consumer 2, work item 2; Consumer 3, work item 2;
 Consumer 1, work item 3; Consumer 2, work item 3; Consumer 3, work item 3;

Parallelism Modes

By default, work items are processed in parallel, with each consumer being run in sequential order ('work parallel').

If you need to have each work item handled strictly in-order, but each consumer can be run in parallel, use the 'task parallel' mode:

dispatcher = Dispatch<string>.CreateTaskParallelMultiThreaded("MyService");
dispatcher.AddConsumer(wi=> Console.Write($" Consumer 1, work item {wi};"));
dispatcher.AddConsumer(wi=> Console.Write($" Consumer 2, work item {wi};"));
dispatcher.AddConsumer(wi=> Console.Write($" Consumer 3, work item {wi};"));
dispatcher.Start();
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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

This package has no dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DispatchSharp:

Package Downloads
SevenDigital.Messaging

A distributed contracts-based sender/handler messaging system built on RabbitMQ and BearBones-Messaging

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.5.0 290 6/12/2023
1.4.0 156 5/26/2023
1.3.0 175 5/10/2023
1.2.2 184 4/25/2023
1.2.1 197 4/13/2023
1.2.0 194 4/13/2023
1.1.3 303 2/1/2023
1.1.2 365 11/2/2022
1.1.0 364 11/1/2022
1.0.0 3,070 8/10/2018
0.1.11 2,814 12/6/2013
0.1.10 1,312 8/19/2013
0.1.9 1,561 7/18/2013
0.1.8 1,443 7/9/2013
0.1.7 1,393 6/19/2013
0.1.6 1,381 6/17/2013
0.1.5 1,350 6/10/2013
0.1.4 1,307 6/6/2013
0.1.3 1,324 6/6/2013
0.1.2 1,309 6/4/2013
0.1.1 1,303 6/4/2013
0.1.0 1,374 5/31/2013
0.0.1 1,364 5/21/2013

Added better shut-down behaviour for polling work queues