Skip to content
StephenCleary edited this page Sep 28, 2014 · 3 revisions

AsyncEx is a library designed to assist with programming when using the async and await keywords.

Asynchronous Coordination Primitives

Most people just need AsyncLock, but AsyncEx also includes AsyncManualResetEvent, AsyncAutoResetEvent, AsyncConditionVariable, AsyncMonitor, AsyncSemaphore, AsyncCountdownEvent, AsyncBarrier, and AsyncReaderWriterLock.

Asynchronous and Concurrent Collections

AsyncProducerConsumerQueue is an async-ready producer/consumer queue that works on all platforms.

AsyncCollection is an async-compatible wrapper around IProducerConsumerCollection, in the same way that BlockingCollection is a blocking wrapper around IProducerConsumerCollection.

You can also use ProducerProgress to send IProgress updates to a producer/consumer collection.

AsyncLazy

The AsyncLazy class provides support for asynchronous lazy initialization.

AsyncContext

The AsyncContext class and the related AsyncContextThread class provide contexts for asynchronous operations for situations where such a context is lacking (i.e., Console applications and Win32 services).

Asynchronous events

DeferralManager simplifies the writing of "command-style" events that provide a GetDeferral method.

MVVM

NotifyTaskCompletion is a data-binding-friendly wrapper for Task, providing property-change notifications when the task completes.

Interop

The AsyncFactory classes interoperate between Task-based Asynchronous Programming (async) and the Asynchronous Programming Model (IAsyncResult). AsyncFactory<T> can also create wrapper tasks for most events.

TaskCompletionSource is a non-generic version of the built-in TaskCompletionSource<TResult>.

TaskCompletionSourceExtensions includes TryCompleteFromEventArgs, which helps interoperate between Task-based Asynchronous Programming (async) and Event-based Asynchronous Programming (*Completed).

Utility Types and Methods

You can use OrderByCompletion to order tasks by when they complete.

CancellationTokenHelpers provides some static methods and constants for working with CancellationTokens.

Miscellaneous

TaskConstants provides static constant Task<TResult> values that are useful when taking advantage of the async fast path.

You can respond to task progress updates by using one of the implementations of IProgress<T>: PropertyProgress, ProducerProgress, or DataflowProgress.

ObservableProgress, an IProgress<T> implementation that exposes progress updates as an IObservable<T> stream, is available as a gist.

Extension Methods

CancellationToken.AsTask will create a task that is canceled when a CancellationToken is canceled.

TaskCompletionSourceExtensions has several extension methods for TaskCompletionSource, including propagating results from a completed Task or AsyncCompletedEventArgs, and for completing the source forcing background continuations.

TaskFactoryExtensions adds Run overloads to task factories.

Dataflow Support

There is currently only one Dataflow block in the library: FuncBlock, which allows an async function to provide data to a dataflow mesh with full cooperative cancellation and error propagation.

Low-Level Building Blocks

ExceptionHelpers.PrepareForRethrow will preserve the stack trace when rethrowing any exception.

For dealing with SynchronizationContext directly, the library provides CurrentOrDefault and SynchronizationContextSwitcher.