AsyncAutoResetEvent Class

Represents an asynchronous auto-reset event that signals a single waiting task when set.

public sealed class AsyncAutoResetEvent

Example

var autoResetEvent = new AsyncAutoResetEvent(initialState: false);

// In one task
var waitingTask = Task.Run(async () =>
{
    await autoResetEvent.WaitAsync();
    return "Event was set!";
});

// In another task
autoResetEvent.Set();
Assert.Equal("Event was set!", await waitingTask);

Constructors

NameDescription
AsyncAutoResetEvent(bool)Initializes a new instance of the AsyncAutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled.
AsyncAutoResetEvent(bool, bool)Initializes a new instance of the AsyncAutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled and whether to allow inlining of continuations.

AsyncAutoResetEvent overloads

AsyncAutoResetEvent(bool)

AsyncAutoResetEvent.cs

Initializes a new instance of the AsyncAutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled.

public AsyncAutoResetEvent(bool initialState)
Parameters
initialState bool
true to set the initial state to signaled; false to set the initial state to non-signaled.

AsyncAutoResetEvent(bool, bool)

AsyncAutoResetEvent.cs

Initializes a new instance of the AsyncAutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled and whether to allow inlining of continuations.

public AsyncAutoResetEvent(bool initialState, bool allowInliningAwaiters)
Parameters
initialState bool
true to set the initial state to signaled; false to set the initial state to non-signaled.
allowInliningAwaiters bool
true to allow continuations to be executed synchronously on the thread that calls AsyncAutoResetEvent.Set; false to execute continuations asynchronously.

Methods

NameDescription
Set()Sets the state of the event to signaled, allowing one waiting task to proceed.
WaitAsync()Asynchronously waits for the event to be set.
WaitAsync(CancellationToken)Asynchronously waits for the event to be set.

Sets the state of the event to signaled, allowing one waiting task to proceed.

public void Set()

WaitAsync overloads

Asynchronously waits for the event to be set.

public Task WaitAsync()
Returns

Task A task that completes when the event is set.

WaitAsync(CancellationToken)

AsyncAutoResetEvent.cs

Asynchronously waits for the event to be set.

public Task WaitAsync(CancellationToken cancellationToken)
Parameters
cancellationToken CancellationToken
A cancellation token to observe while waiting.
Returns

Task A task that completes when the event is set.