CenterEdge.Async 1.2.0

dotnet add package CenterEdge.Async --version 1.2.0                
NuGet\Install-Package CenterEdge.Async -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CenterEdge.Async" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CenterEdge.Async --version 1.2.0                
#r "nuget: CenterEdge.Async, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CenterEdge.Async as a Cake Addin
#addin nuget:?package=CenterEdge.Async&version=1.2.0

// Install CenterEdge.Async as a Cake Tool
#tool nuget:?package=CenterEdge.Async&version=1.2.0                

CenterEdge.Async

When you gotta, you gotta.

Don't use this library unless you gotta. Running async code from sync code tends to result in deadlocks, thread pool depletion, poor performance, and unexpected behaviors. However, when gradually converting code from sync to async sometimes you must to keep the scope of work under control.

Usage

// Basic usage for an action running Task or ValueTask
AsyncHelper.RunSync(() => SomeFunctionAsync(param1, param2));

// Basic usage for an action running Task<T> or ValueTask<T>
var result = AsyncHelper.RunSync(() => SomeFunctionAsync(param1, param2));

// Pass in state as a parameter to reduce heap allocations due to closures
AsyncHelper.RunSync(state => SomeFunctionAsync(state, true), param1);
// or you can use a method group if parameters align precisely
AsyncHelper.RunSync(SomeFunctionAsyncWithOneParameter, param1);

Background

This project is based off this post. The original flavor had the following behaviors:

  • The calling thread is blocked until the asynchronous work is completed
  • Basic await operations will run the continuation on the calling thread
    • Awaiting with .ContinueAwait(false) may run the continuations on the thread pool. However, this is not guaranteed, the continuation may be executed synchronously if the awaited task is returned already complete.
  • The result of the asynchronous task is returned, if applicable
  • Exceptions returned by the asynchronous task will be thrown

Improvements

This implementation offers the following improvements over the original implementation:

  • Support for direct usage of ValueTask and ValueTask<T>. This avoids the need to call .AsTask() and offers better performance in the case where the ValueTask is returned already completed.
  • Exceptions are thrown with a more meaningful stack trace by using .GetResult() on the awaiter.
  • SynchronizationContext.Current is now correctly reset if the asynchronous work returns an exception.
    • This can prevent some esoteric deadlock scenarios, especially in cases like WinForms or WPF which use a single long-lived synchronization context.
  • Asynchronous work left running after the main task completes might never execute their continuations unless they were awaited with .ContinueAwait(false). These continuations will now be run on the SynchronizationContext captured at the time RunSync is called.
  • Dispose is used to perform cleanup which can reduce Gen1 and Gen2 garbage collections on objects with finalizers.
  • Significant overall performance improvements.
    • For example, benchmarks are showing a 78% improvement on .NET 4.8 running in x86 (75% in x64).

Limitations

This implementation still has several limitations. As always, writing truly asynchronous code is the best approach.

  • Work will still be single-threaded in most cases, potentially reducing performance.
  • The calling thread is blocked, which can lead to thread pool depletion issues.
  • In scenarios where many calls to RunSync are running at once in different threads, the ThreadPool may need to scale up to a larger size which can reduce performance due to context switching.
  • RunSync itself adds some additional overhead, though this implementation has less impact than the original.

Running Benchmarks

cd src/CenterEdge.Async.Benchmarks

# x64
dotnet run -c Release -f net8.0 -- job default --runtimes net48 net6.0 net8.0

# x86, .NET 4.8 only
dotnet run -c Release -f net8.0 -- job default --runtimes net48 --platform x86
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 4,902 12/10/2023