AsyncManualResetEvent Class
Definition
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
Properties
IsSet
Gets a value indicating whether the event is currently signaled.
public bool IsSet { get; }
Property Value
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
WaitAsync(CancellationToken)
Asynchronously waits until the event is signaled, observing a cancellation request while waiting.
public ValueTask WaitAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenA token used to cancel the wait.
Returns
Remarks
Cancellation affects only the calling waiter; other waiters on the same event are unaffected.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before the event was set.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |