Table of Contents

AsyncAutoResetEvent Class

Definition

Namespace
Bodu.Threading
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
AsyncAutoResetEvent.cs

Provides an asynchronous auto-reset signaling primitive: each call to Set() releases exactly one waiter and then automatically returns to the unsignaled state.

public sealed class AsyncAutoResetEvent
Inheritance
AsyncAutoResetEvent
Inherited Members
Extension Methods

Examples

private readonly AsyncAutoResetEvent _itemReady = new();

// Producer: each Set releases exactly one waiter (or latches a single signal).
void OnItemEnqueued() => _itemReady.Set();

// Consumer: wake once per signal and process a single item at a time.
async Task ConsumeAsync(CancellationToken token)
{
    while (!token.IsCancellationRequested)
    {
        await _itemReady.WaitAsync(token);
        ProcessNextItem();
    }
}

Remarks

AsyncAutoResetEvent is the asynchronous analogue of AutoResetEvent. When a waiter is queued, Set() releases the longest-waiting caller (strict FIFO) and consumes the signal. When no waiter is queued, the signal is latched so that the next WaitAsync() completes immediately; the event holds at most one pending signal.

Waiters are tracked in a FIFO queue of TaskCompletionSource<TResult> instances created with RunContinuationsAsynchronously, so the thread calling Set() never runs a waiter's continuation inline. The type owns no operating-system handle and does not implement IDisposable.

Cancellation follows the package-wide rule: a signal that is already latched is consumed even when the supplied token is already canceled. A token only cancels a wait that cannot complete immediately.

Constructors

AsyncAutoResetEvent()

Initializes a new instance of the AsyncAutoResetEvent class in the unsignaled state.

public AsyncAutoResetEvent()

AsyncAutoResetEvent(bool)

Initializes a new instance of the AsyncAutoResetEvent class.

public AsyncAutoResetEvent(bool initialState)

Parameters

initialState bool

true to create the event with a pending signal; otherwise, false.

Methods

Set()

Signals the event, releasing exactly one waiter or latching the signal when no waiter is queued.

public void Set()

WaitAsync()

Asynchronously waits for the event to be signaled, consuming the signal.

public ValueTask WaitAsync()

Returns

ValueTask

A ValueTask that completes when this caller receives the signal.

Remarks

The returned ValueTask must be awaited exactly once.

WaitAsync(CancellationToken)

Asynchronously waits for the event to be signaled, consuming the signal and observing a cancellation request while waiting.

public ValueTask WaitAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

A token used to cancel the pending wait.

Returns

ValueTask

A ValueTask that completes when this caller receives the signal.

Remarks

The returned ValueTask must be awaited exactly once. A latched signal is consumed even when cancellationToken is already canceled; the token only cancels a wait that must queue, and cancellation removes only the calling waiter from the queue.

Exceptions

OperationCanceledException

cancellationToken was canceled before the signal was received.

Applies to

ProductVersions
.NET8, 10