A helper library for async/await.
C# PowerShell
Permalink
Failed to load latest commit information.
Future Add future notes. Feb 21, 2015
Source Fix build and versions. Nov 7, 2016
.gitignore Bumped version and updated build support. Feb 22, 2015
AsyncEx.128.png Added icon. Sep 1, 2014
LICENSE Initial commit May 8, 2014
README.md Update README.md Jan 20, 2015

README.md

Logo

AsyncEx

A helper library for async/await.

Supports .NET 4.5/4.0, iOS, Android, Windows Store 8.0, Windows Phone Silverlight 8.0/7.5, Windows Phone Applications 8.1, Silverlight 5.0/4.0, and all portable libraries thereof.

Note: iOS applications need to add a special line of code to prevent aggresive compiler optimizations:

EnlightenmentVerification.EnsureLoaded();

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, AsyncBarrier, and AsyncReaderWriterLock.

MVVM Support

NotifyTaskCompletion is a data-binding-friendly wrapper for a Task, raising property-change notifications when the task completes. PropertyProgress exposes progress updates as property-change events.

More Stuff

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

Infrequently Asked Questions

Strong Naming

Need strong-naming? Use the assembly strong naming toolkit.