PosInformatique.Foundations.Threading 1.7.0

Prefix Reserved
dotnet add package PosInformatique.Foundations.Threading --version 1.7.0
                    
NuGet\Install-Package PosInformatique.Foundations.Threading -Version 1.7.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="PosInformatique.Foundations.Threading" Version="1.7.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PosInformatique.Foundations.Threading" Version="1.7.0" />
                    
Directory.Packages.props
<PackageReference Include="PosInformatique.Foundations.Threading" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PosInformatique.Foundations.Threading --version 1.7.0
                    
#r "nuget: PosInformatique.Foundations.Threading, 1.7.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.
#:package PosInformatique.Foundations.Threading@1.7.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PosInformatique.Foundations.Threading&version=1.7.0
                    
Install as a Cake Addin
#tool nuget:?package=PosInformatique.Foundations.Threading&version=1.7.0
                    
Install as a Cake Tool

PosInformatique.Foundations.Threading

NuGet version NuGet downloads

Introduction

This library provides utilities and extensions related to the System.Threading APIs of .NET. It supports .NET 8, .NET 9, .NET 10 and .NET 11, trimming and Native AOT.

Introduced in v1.7.0:

  • Debouncer delays an asynchronous action and cancels the previous invocation whenever a new invocation is requested.
  • SemaphoreSlimExtensions.EnterScopeAsync() pairs WaitAsync() and Release() through a disposable scope.

Debouncer is available in the PosInformatique.Foundations.Threading namespace; SemaphoreSlimExtensions is available in System.Threading.

Install

dotnet add package PosInformatique.Foundations.Threading

Debouncer

Usage

using PosInformatique.Foundations.Threading;

using var debouncer = new Debouncer(
    async token =>
    {
        await Task.Delay(TimeSpan.FromMilliseconds(50), token);
        Console.WriteLine("Action executed.");
    },
    TimeSpan.FromMilliseconds(300));

var first = debouncer.InvokeAsync();
var second = debouncer.InvokeAsync();

// Only the latest pending invocation executes the action.
await Task.WhenAll(first, second);

Behavior

  • Each InvokeAsync() cancels the previous invocation and restarts the delay.
  • The action receives a cancellation token and should observe it. Cancellation is cooperative: a running action that ignores its token may overlap a newer action.
  • IsInProgress indicates whether the latest invocation is waiting or executing. It does not track older actions that continue after cancellation.
  • Cancellation requested by a newer invocation or Dispose() completes the invocation normally. Other exceptions, including unrelated cancellation, are propagated to the caller.
  • Dispose() requests cancellation without waiting for completion. Await the invocation tasks to observe their completion and any exceptions, including exceptions from cancellation callbacks.
  • Calling InvokeAsync() after disposal throws ObjectDisposedException.
  • A null action throws ArgumentNullException. The delay must be between zero and 4,294,967,294 milliseconds; otherwise, the constructor throws ArgumentOutOfRangeException.
  • Concurrent invocations and disposal are synchronized.

SemaphoreSlim scopes

EnterScopeAsync() waits asynchronously for a semaphore and returns an IDisposable scope. Use a regular using statement with the awaited scope; disposing it calls Release() synchronously.

using System.Threading;

using var semaphore = new SemaphoreSlim(1, 1);

using (await semaphore.EnterScopeAsync())
{
    await Task.Delay(TimeSpan.FromMilliseconds(50));
    // Work protected by the semaphore.
}

Pass a cancellation token to cancel the wait:

using (await semaphore.EnterScopeAsync(cancellationToken))
{
    await DoWorkAsync(cancellationToken);
}

Behavior

  • The scope is returned only after acquisition succeeds.
  • Cancellation or failure during acquisition does not release the semaphore.
  • Cancellation after acquisition does not release the semaphore automatically.
  • Leaving the using block releases the semaphore, including when the protected code throws.
  • Repeated or concurrent calls to the same scope's Dispose() release the semaphore at most once.
  • The scope does not dispose the semaphore. Keep the semaphore alive until all scopes and waiters finish.
  • A null semaphore throws ArgumentNullException; a disposed semaphore throws ObjectDisposedException.
  • Do not manually call Release() for an acquisition owned by a scope.

Changelog

See CHANGELOG.md for release history.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net11.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.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.7.0 51 11/23/2026
1.7.0-rc.1 50 11/23/2026

1.7.0
 - Initial release with utilities related to System.Threading.
 - Added Debouncer to debounce asynchronous actions with cooperative cancellation.
 - Added SemaphoreSlimExtensions.EnterScopeAsync() to create an asynchronous Wait/Release scope with cancellation support and idempotent disposal.