AsyncEx is an open-source library developed in C# by Stephen Cleary that simplifies asynchronous programming in .NET.
Asynchronous programming always has its difficulties to implement correctly. This leads to errors and unwanted behaviors that are also very difficult to detect.
This is where AsyncEx comes in. It provides a variety of types and methods that facilitate the writing of asynchronous code, reduce complexity, and improve performance.
Among the different utilities it provides, the most popular and well-known are AsyncLock, which allows locking in an asynchronous task, and AsyncManualResetEvent, which allows event synchronization between multiple threads.
Other features include asynchronous and concurrent collections, AsyncMonitor, AsyncSemaphore, AsyncCountdownEvent, and AsyncReaderWriterLock, AsyncLazy, and AsyncContext.
How to use AsyncEx
We can easily add the library to a .NET project through the corresponding Nuget package.
Install-Package Nito.AsyncEx
Here’s an example of how to use AsyncLock from AsyncEx taken from the library’s documentation
private readonly AsyncLock _mutex = new AsyncLock();
public async Task UseLockAsync()
{
// AsyncLock can be locked asynchronously
using (await _mutex.LockAsync())
{
// It's safe to await while the lock is held
await Task.Delay(TimeSpan.FromSeconds(1));
}
}
AsyncEx is Open Source, and all the code and documentation is available in the project’s repository at https://github.com/StephenCleary/AsyncEx