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
<PackageReference Include="PosInformatique.Foundations.Threading" Version="1.7.0" />
<PackageVersion Include="PosInformatique.Foundations.Threading" Version="1.7.0" />
<PackageReference Include="PosInformatique.Foundations.Threading" />
paket add PosInformatique.Foundations.Threading --version 1.7.0
#r "nuget: PosInformatique.Foundations.Threading, 1.7.0"
#:package PosInformatique.Foundations.Threading@1.7.0
#addin nuget:?package=PosInformatique.Foundations.Threading&version=1.7.0
#tool nuget:?package=PosInformatique.Foundations.Threading&version=1.7.0
PosInformatique.Foundations.Threading
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:
Debouncerdelays an asynchronous action and cancels the previous invocation whenever a new invocation is requested.SemaphoreSlimExtensions.EnterScopeAsync()pairsWaitAsync()andRelease()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.
IsInProgressindicates 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 throwsObjectDisposedException. - A null action throws
ArgumentNullException. The delay must be between zero and 4,294,967,294 milliseconds; otherwise, the constructor throwsArgumentOutOfRangeException. - 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
usingblock 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 throwsObjectDisposedException. - Do not manually call
Release()for an acquisition owned by a scope.
Changelog
See CHANGELOG.md for release history.
Links
| Product | Versions 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. |
-
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.