AsyncAutoResetEvent Class
Represents an asynchronous auto-reset event that signals a single waiting task when set.
public sealed class AsyncAutoResetEventExample
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
| Name | Description |
|---|---|
| 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.csInitializes 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
initialStatebooltrueto set the initial state to signaled;falseto set the initial state to non-signaled.
AsyncAutoResetEvent(bool, bool)
AsyncAutoResetEvent.csInitializes 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.
Parameters
initialStatebooltrueto set the initial state to signaled;falseto set the initial state to non-signaled.allowInliningAwaitersbooltrueto allow continuations to be executed synchronously on the thread that callsAsyncAutoResetEvent.Set;falseto execute continuations asynchronously.
Methods
| Name | Description |
|---|---|
| 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
WaitAsync()
AsyncAutoResetEvent.csWaitAsync(CancellationToken)
AsyncAutoResetEvent.csAsynchronously waits for the event to be set.
public Task WaitAsync(CancellationToken cancellationToken)Parameters
cancellationTokenCancellationToken- A cancellation token to observe while waiting.
Returns
Task A task that completes when the event is set.