ExtendedThreading 1.2.1

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

// Install ExtendedThreading as a Cake Tool
#tool nuget:?package=ExtendedThreading&version=1.2.1

ExtendedThreading

This package provides extended Threading functionality, built on top of the built-in Threading capabilities of .Net.

Installation

I recommend using the NuGet package: ExtendedThreading however feel free to clone the source instead if that suits your needs better.

Usage

ThreadSignal

This is used to simplify signalling between threads, e.g. when building the Producer/Consumer pattern:

public class ProducerConsumer<T>
{
	private ThreadSignal _signal = new();
	
	public void Produce(T item){
		// produce
		_signal.Pulse(); // Inform consumers that a new item is available
	}

	public void Consume()
	{
		_signal.Wait(); // Will block until an item becomes available
		// consume
	}
}

KeyedMutexSynchronizer

This is used to ensure mutual exclusion based on keys. E.g. for an API where you want to grant only a single thread access to do PUT requests on a per-id basis to prevent race conditions on a per entity basis:

public class OrderController
{
	private readonly KeyedMutexSynchronizer<OrderId> _synchronizer;
	private readonly IOrderService _orderService;

	public OrderController(IOrderService orderService, KeyedMutexSynchronizer<OrderId> synchronizer)
	{
		_synchronizer = synchronizer;
		_orderService = orderService;
	}

	[HttpPut("{id})]
	public async Task PutAsync(OrderId id, OrderModel model, CancellationToken cancellationToken)
	{
		// The code in the lambda expression will be protected by a mutex based on OrderId, no two requests with the same OrderId will execute simultaneously, however two requests with different OrderIds will execute simultaneously like normal
		await _synchronizer.InvokeSynchronizedActionAsync(id, async () => { 
			var order = _orderService.GetOrderAsync(id, cancellationToken);
			order.Update(model)
			await _orderService.PersistOrderAsync(order, cancellationToken);
		}, cancellationToken);
	}
}

TaskExtension

This class only offers one method: WhenAll. It functions similarly to the built-in Task.WhenAll in .Net, except for how it handles Exceptions.

This version throws an AggregateException in case any exceptions occur, to allow you the full picture of all exceptions, instead of just the first one (which is what the built-in Task.WhenAll will throw)

AwaitExtensions

This isn't actually called directly, rather it supports awaiting tuples, even of mixed types, e.g.

public Task<string> HelloWorldAsync(); // Returns "Hello world"
public Task<int> MeaningOfLifeAsync(); // Returns 42

var task1 = HelloWorldAsync();
var task2 = MeaningOfLifeAsync();

var results = await (task1, task2);
// results will be "Hello world" and 42 respectively
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

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.2.1 11,705 11/10/2023
1.2.0 78 11/8/2023
1.1.0 578 9/11/2023
1.0.0 148 9/10/2023