Show / Hide Table of Contents

Async Reset Event

AsyncManualResetEvent and AsyncAutoResetEvent are asynchronous alternatives to ManualResetEvent and AutoResetEvent with the same semantics. However, it doesn't support interoperation with .NET thread pool and native code because there is no wait handle associated with an instance of event.

using DotNext.Threading;
using System;
using System.Threading;

var resetEvent = new AsyncManualResetEvent(false);

Task.Factory.StartNew(async () =>
{
    Console.WriteLine("Waiting for parent task");
    await resetEvent.WaitAsync();
    Console.WriteLine("Task #1 finished");
});

Task.Factory.StartNew(async () =>
{
    Console.WriteLine("Waiting for parent task");
    await resetEvent.WaitAsync();
    Console.WriteLine("Task #2 finished");
});

resetEvent.Set();   //allow the tasks to complete their job

AsyncAutoResetEvent respects the same fairness policy as well as other asynchronous locks. For AsyncManualResetEvent it is not relevant because all suspended waiters will be released when event occurred.

Synchronous wait

The class exposes Wait(TimeSpan) blocking method that can be used by synchronous callers. The method allows to perform mixed synchronization for synchronous and asynchronous code at the same time.

  • Edit this page
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾