Skip to main content
.NETFramework4.8net8.0net9.0net10.0.NETStandard2.0

This lightweight library adds await support for ordinary WaitHandle objects such as ManualResetEvent, AutoResetEvent, and others.

Published
Nov 13, 2025
11mo ago
Total Downloads
597
597

Install

dotnet add package AwaitExtensions --version 1.0.0
ProductVersions
.NET
net10.0net8.0net9.0
.NET Standard
netstandard2.0
Other
.NETFramework4.8

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() β€” Enables await handle;
  • ToTask() β€” Converts a WaitHandle into a Task that completes when the handle is signaled

This makes it easy to integrate traditional wait-based APIs into modern async/await workflows.


✨ Features

  • Await any WaitHandle directly
  • Fully compatible with async/await patterns
  • 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.RegisterWaitForSingleObject to react to the signal
  • TaskCompletionSource<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.

Compatibility metadata lists 5 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.