Skip to content
A helper library for async/await.
C# PowerShell
Branch: master
Clone or download
Latest commit 76b27a3 Apr 10, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
doc Update design.md Feb 26, 2019
nuspec v5.0.0 Apr 2, 2019
src v5.0.0 Apr 2, 2019
test Upgrade build system. Feb 26, 2019
.gitignore Upgrade build system. Feb 26, 2019
AsyncEx.128.png Added icon. Sep 1, 2014
Build.ps1 Upgrade build system. Feb 26, 2019
CONTRIBUTING.md Add contributing. Dec 29, 2018
Coverage.ps1 Upgrade build system. Feb 26, 2019
LICENSE Initial commit May 8, 2014
Nito.AsyncEx.sln Add global.json. Dec 28, 2018
README.md Update README.md Apr 10, 2019
Version.ps1 Upgrade build system. Feb 26, 2019
appveyor.yml Upgrade build system. Feb 26, 2019
global.json Add global.json. Dec 28, 2018

README.md

Logo

AsyncEx

A helper library for async/await.

Note: This README is for AsyncEx v5 (the current version). For AsyncEx v4, see here.

Supports netstandard1.3 (including .NET 4.6, .NET Core 1.0, Xamarin.iOS 10, Xamarin.Android 7, Mono 4.6, and Universal Windows 10).

NuGet Pre Release netstandard 1.3 netstandard 2.0 Code Coverage Build status

API docs

Overview - Upgrade Guide

Getting Started

Install the NuGet package.

AsyncLock

A lot of developers start using this library for AsyncLock, an async-compatible mutual exclusion mechanism. Using AsyncLock is straightforward:

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));
  }
}

AsyncLock also fully supports cancellation:

public async Task UseLockAsync()
{
  // Attempt to take the lock only for 2 seconds.
  var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
  
  // If the lock isn't available after 2 seconds, this will
  //  raise OperationCanceledException.
  using (await _mutex.LockAsync(cts.Token))
  {
    await Task.Delay(TimeSpan.FromSeconds(1));
  }
}

AsyncLock also has a synchronous API. This permits some threads to acquire the lock asynchronously while other threads acquire the lock synchronously (blocking the thread).

public async Task UseLockAsync()
{
  using (await _mutex.LockAsync())
  {
    await Task.Delay(TimeSpan.FromSeconds(1));
  }
}

public void UseLock()
{
  using (_mutex.Lock())
  {
    Thread.Sleep(TimeSpan.FromSeconds(1));
  }
}

Other Coordination Primitives

AsyncLock is just the beginning. The AsyncEx library contains a full suite of coordination primitives: AsyncManualResetEvent, AsyncAutoResetEvent, AsyncConditionVariable, AsyncMonitor, AsyncSemaphore, AsyncCountdownEvent, and AsyncReaderWriterLock.

More Stuff

There's quite a few other helpful types; see the docs for full details

Infrequently Asked Questions

Older Platforms

AsyncEx v4 supported .NET 4.0, Windows Store 8.1, Windows Phone Silverlight 8.0, Windows Phone Applications 8.1, and Silverlight 5.0. Support for these platforms has been dropped with AsyncEx v5.

AsyncEx v3 supported Windows Store 8.0, Windows Phone Silverlight 7.5, and Silverlight 4.0. Support for these platforms has been dropped with AsyncEx v4.

You can’t perform that action at this time.