Table of Contents

AsyncManualResetEvent Class

Definition

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

Provides an asynchronous, manually reset signaling primitive: once set, every current and future waiter is released until the event is explicitly reset.

public sealed class AsyncManualResetEvent
Inheritance
AsyncManualResetEvent
Inherited Members
Extension Methods

Examples

private readonly AsyncManualResetEvent _ready = new();

// Many callers await the same gate; one Set releases all of them.
async Task UseServiceAsync()
{
    await _ready.WaitAsync();
    CallService();
}

void OnInitialized() => _ready.Set();    // open the gate for everyone
void OnConnectionLost() => _ready.Reset(); // later waiters block again

Remarks

AsyncManualResetEvent is the asynchronous analogue of ManualResetEventSlim. Callers await WaitAsync(); a call to Set() completes all outstanding waits and leaves the event signaled so that subsequent waits complete immediately. Reset() returns the event to the unsignaled state.

The event is backed by a single shared TaskCompletionSource<TResult> created with RunContinuationsAsynchronously, so the thread that calls Set() is never hijacked to run waiter continuations inline.

Cancellation follows the package-wide rule: when the event is already set, WaitAsync() completes immediately even if the supplied token is already canceled. A Set() that races with a Reset() is observed consistently: a waiter that captured the completed task before the reset still completes, while a wait that begins after the reset observes the new unsignaled state.

Constructors

AsyncManualResetEvent()

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

public AsyncManualResetEvent()

AsyncManualResetEvent(bool)

Initializes a new instance of the AsyncManualResetEvent class.

public AsyncManualResetEvent(bool initialState)

Parameters

initialState bool

true to create the event in the signaled state; otherwise, false.

Properties

IsSet

Gets a value indicating whether the event is currently signaled.

public bool IsSet { get; }

Property Value

bool

true if the event is set; otherwise, false.

Methods

Reset()

Resets the event to the unsignaled state. Has no effect if the event is already unsignaled.

public void Reset()

Set()

Sets the event, releasing every current and future waiter until the event is reset.

public void Set()

WaitAsync()

Asynchronously waits until the event is signaled.

public ValueTask WaitAsync()

Returns

ValueTask

A ValueTask that completes when the event is set.

WaitAsync(CancellationToken)

Asynchronously waits until the event is signaled, observing a cancellation request while waiting.

public ValueTask WaitAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

A token used to cancel the wait.

Returns

ValueTask

A ValueTask that completes when the event is set.

Remarks

Cancellation affects only the calling waiter; other waiters on the same event are unaffected.

Exceptions

OperationCanceledException

cancellationToken was canceled before the event was set.

Applies to

ProductVersions
.NET8, 10