Install
dotnet add package AsyncWaitHandle2 --version 2.0.1
| Product | Versions |
|---|---|
| .NET Standard |
README
Use C#/VB await keyword with AutoResetEvent, ManualResetEvent, or Semaphore in .NET apps
Extension methods for the WaitHandle class in the System.Threading namespace for your convenience.
Available as NuGet package 'AsyncWaitHandle2' on nuget.org: https://www.nuget.org/packages/AsyncWaitHandle2/
Example 1 (simple await):
var e = new AutoResetEvent();
...
await e;
Example 2 (configured await):
var e = new AutoResetEvent();
var cts = new CancellationTokenSource();
...
var timeout = TimeSpan.FromSeconds(30);
try
{
await e.ConfigureAwait(timeout, cts.Token);
}
catch (TimeOutException)
{
...
}
catch (OperationCanceledException)
{
...
}
Example 3 (WaitOneAsync):
var e = new AutoResetEvent();
var cts = new CancellationTokenSource();
...
var timeout = TimeSpan.FromSeconds(30);
bool fSignaled;
try
{
fSignaled = await e.WaitOneAsync(timeout, cts.Token);
}
catch (OperationCanceledException)
{
...
}
Example 4 (WaitAnyAsync):
var events = new WaitHandle[] { new AutoResetEvent(), new ManualResetEvent() };
var cts = new CancellationTokenSource();
...
var timeout = TimeSpan.FromSeconds(30);
int signaledIndex;
try
{
signaledIndex = await events.WaitAnyAsync(timeout, cts.Token);
}
catch (OperationCanceledException)
{
...
}
switch (signaledIndex)
{
case 0: ...
case 1: ...
case WaitHandle.Wait...
}
Example 5 (WaitAllAsync):
var events = new WaitHandle[] { new AutoResetEvent(), new ManualResetEvent() };
var cts = new CancellationTokenSource();
...
var timeout = TimeSpan.FromSeconds(30);
bool allSignaled;
try
{
allSignaled = await events.WaitAllAsync(timeout, cts.Token);
}
catch (OperationCanceledException)
{
...
}
Changes from Previous Version
No structural changes between v2.0.0 and v2.0.1.
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Compatibility metadata lists 1 target framework; confirm the exact target used by your application.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.