Gapotchenko.FX.Threading 2022.2.5

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Gapotchenko.FX.Threading --version 2022.2.5                
NuGet\Install-Package Gapotchenko.FX.Threading -Version 2022.2.5                
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="Gapotchenko.FX.Threading" Version="2022.2.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gapotchenko.FX.Threading --version 2022.2.5                
#r "nuget: Gapotchenko.FX.Threading, 2022.2.5"                
#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 Gapotchenko.FX.Threading as a Cake Addin
#addin nuget:?package=Gapotchenko.FX.Threading&version=2022.2.5

// Install Gapotchenko.FX.Threading as a Cake Tool
#tool nuget:?package=Gapotchenko.FX.Threading&version=2022.2.5                

Overview

The module provides extended primitives for multithreaded and asynchronous programming.

TaskBridge

TaskBridge class from Gapotchenko.FX.Threading module provides seamless interoperability between synchronous and asynchronous code execution models.

Executing an async task from synchronous code poses a few rather big challenges in conventional .NET:

  • The wait operation for an async task is prone to deadlocks unless a proper synchronization context is in place
  • The cancellation models of sync and async code are different and often incompatible

Meet TaskBridge. It makes interoperability a breeze:

using Gapotchenko.FX.Threading.Tasks;
using System;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        TaskBridge.Execute(RunAsync);
    }

    static async Task RunAsync()
    {
        await Console.Out.WriteLineAsync("Hello, Async World!");
    }
}

Cancellation Models

TaskBridge provides automatic interoperability between different cancellation models.

Let's call a cancelable async method from a synchronous thread that can be aborted by Thread.Abort() method:

using Gapotchenko.FX.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;

void SyncMethod() // can be canceled by Thread.Abort()
{
    // Executes an async task that is gracefully canceled via cancellation
    // token when current thread is being aborted or interrupted.
    TaskBridge.Execute(DoJobAsync); // <-- TaskBridge DOES THE MAGIC
}

async Task DoJobAsync(CancellationToken ct)
{
    …
    // Gracefully handles cancellation opportunities.
    ct.ThrowIfCancellationRequested();
    …
}

You see this? A simple one-liner for a complete interoperability between two execution models.

Now, let's take a look at the opposite scenario where a cancelable async task calls an abortable synchronous code:

using Gapotchenko.FX.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;

async Task DoJobAsync(CancellationToken ct) // can be canceled by a specified cancellation token
{
    // Executes a synchronous method that is thread-aborted when
    // a specified cancellation token is being canceled.
    await TaskBridge.ExecuteAsync(SyncMethod, ct); // <-- TaskBridge DOES THE MAGIC
}

void SyncMethod()
{
    …
}

As you can see, TaskBridge has a lot of chances to become your tool #1, as it elegantly solves a world-class problem of bridging sync and async models together.

Sequential, an Antogonist to Parallel

.NET platform provides System.Threading.Tasks.Parallel class that contains a bunch of static methods allowing to execute the tasks in parallel. But what if you want to temporarily switch them to a sequential execution mode?

Of course, you can do that manually, for example, by changing Parallel.ForEach method to foreach C# language keyword. But this is a lot of manual labour prone to errors. That's why Gapotchenko.FX.Threading module provides Sequential class, an anotogonist to Parallel. It allows to make the switch by changing just the class name from Parallel to Sequential in a corresponding function call. So Parallel.ForEach becomes Sequential.ForEach, and voila, the tasks are now executed sequentially allowing you to isolate that pesky multithreading bug you were hunting for.

Commonly Used Types

  • Gapotchenko.FX.Threading.TaskBridge

Other Modules

Let's continue with a look at some other modules provided by Gapotchenko.FX:

Or look at the full list of modules.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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 is compatible.  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 was computed.  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 is compatible.  netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 is compatible.  net472 is compatible.  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 (3)

Showing the top 3 NuGet packages that depend on Gapotchenko.FX.Threading:

Package Downloads
Gapotchenko.FX.Diagnostics.Process

Provides extended functionality for process manipulation.

Gapotchenko.FX.Profiles.Core

Represents the Core profile of Gapotchenko.FX.

Gapotchenko.FX.Data.Linq

Provides asynchronous data access support for LINQ2SQL technology.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2024.1.3 669 11/10/2024
2022.2.7 40,728 5/1/2022
2022.2.5 2,734 5/1/2022
2022.1.4 2,872 4/6/2022
2021.2.21 3,018 1/21/2022
2021.2.20 2,841 1/17/2022
2021.1.5 8,639 7/6/2021
2020.2.2-beta 1,121 11/21/2020
2020.1.15 5,259 11/5/2020
2020.1.9-beta 1,204 7/14/2020
2020.1.8-beta 1,198 7/14/2020
2020.1.7-beta 1,238 7/14/2020
2020.1.1-beta 1,185 2/11/2020
2019.3.7 2,351 11/4/2019
2019.2.20 1,527 8/13/2019
2019.1.151 26,712 3/30/2019