Home

Awesome

MinimumAsyncBridge

Miminum set of the async/await portability libararies.

Usage

NuGet packages:

These packages includes:

So, these libraries are supported in mixed versions with both 3.5 and 4.5 (but not with .NET 4). You can create a library with .NET 3.5 with the back-ports, and then, consume it from an app with .NET 4.5.

Motivation

I'd like to use async/await functionality in Unity game engine. The runtime version of Mono used by Unity is 2.8 which is nearly equivalent to .NET 3.5 and does not have the Task class.

In this background, I have implemented back-porting libararies of the classes related to async/await to .NET 3.5, especially Unity. The back-porting libararies includes the Task, TaskCompletionSource, TaskAwaiter, and so on.

Implementation

The implementation is based on code in https://github.com/Microsoft/referencesource, I use tunnelvisionlabs/dotnet-threading#90 as a reference, and the MinimumAsyncBridge has the same problem as the dotnet-threading.

The Task class in .NET has multiple roles:

  1. threading: Task.Run
  2. timer: Task.Delay
  3. future/promise: TaskCompletionSource

ThE back-porting libararies implement only 2. and 3. (Minimum requirement of async/await is 3.). For 1., only a few overloads of Task.Run, WhenAny and WhenAll are implemented (but, it's simplified implementation). If you would need the full threading capability, you could use another threading library and interop by using TaskCompletionSource.

Known problem

The debbugability of the Task in the MinimumAsyncBridge is poorer than original Task. Stack trace information is lost because the back-port does not have System.Runtime.ExceptionServices.ExceptionDispatchInfo, which is impossible without runtime support.

Notice: Use in Unity

The back-porting libararies contains classes required for async/await. However, the C# 5.0 (or later) compiler is also required. Thus: