AwaitExtensions
This lightweight library adds await support for ordinary WaitHandle objects such as ManualResetEvent, AutoResetEvent, and others.
Install
| Product | Versions |
|---|---|
| .NET | |
| .NET Standard | |
| Other |
README
AwaitExtensions β Awaitable WaitHandle Support for .NET
This lightweight library adds await support for ordinary WaitHandle objects such as ManualResetEvent, AutoResetEvent, and others.
It exposes two simple extension methods:
GetAwaiter()β Enablesawait handle;ToTask()β Converts aWaitHandleinto aTaskthat completes when the handle is signaled
This makes it easy to integrate traditional wait-based APIs into modern async/await workflows.
β¨ Features
- Await any
WaitHandledirectly - Fully compatible with
async/awaitpatterns - Minimal, dependency-free implementation
- Available as a NuGet package
π¦ Installation
Add the NuGet package to your project:
dotnet add package AwaitExtensions
Or install via the NuGet Package Manager UI.
π Usage
Awaiting a WaitHandle
var evt = new ManualResetEvent(false);
// Somewhere in your async method:
await evt; // resumes when the event is signaled
Converting to a Task
var evt = new ManualResetEvent(false);
Task t = evt.ToTask();
await t;
Example
public async Task WaitForSignalAsync()
{
using var evt = new ManualResetEvent(false);
_ = Task.Run(() =>
{
Thread.Sleep(1000);
evt.Set();
});
Console.WriteLine("Waiting...");
await evt;
Console.WriteLine("Signal received!");
}
π§ How It Works
The implementation internally uses:
ThreadPool.RegisterWaitForSingleObjectto react to the signalTaskCompletionSource<object>to complete the returned task- A small lock to ensure safe initialization and unregistration
The result is a task that completes as soon as the handle is signaled.
π Acknowledgements
The core implementation is based on a gist originally published by Andrew Arnott:
π https://gist.github.com/AArnott/1084951
Huge thanks to Andrew for sharing the elegant original version!
This package only wraps and redistributes the same idea for easier consumption.
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.