iszbela.TaskControl 1.2.2

dotnet add package iszbela.TaskControl --version 1.2.2
                    
NuGet\Install-Package iszbela.TaskControl -Version 1.2.2
                    
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="iszbela.TaskControl" Version="1.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="iszbela.TaskControl" Version="1.2.2" />
                    
Directory.Packages.props
<PackageReference Include="iszbela.TaskControl" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add iszbela.TaskControl --version 1.2.2
                    
#r "nuget: iszbela.TaskControl, 1.2.2"
                    
#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.
#:package iszbela.TaskControl@1.2.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=iszbela.TaskControl&version=1.2.2
                    
Install as a Cake Addin
#tool nuget:?package=iszbela.TaskControl&version=1.2.2
                    
Install as a Cake Tool

TaskControl

NuGet: iszbela.TaskControl

It handles the chained control flow of synchronous/asynchronous tasks.

Description:

The package implements a mechanism for chaining async and sync tasks to execute sequentially.

Async tasks are executed concurrently. Whereas async- sync, and sync tasks are alternated, ensuring that each task waits for the completion of the previous task(s) before starting.

Install

# .NET CLI
 dotnet add package iszbela.TaskControl
# Package Manager
 Install-Package iszbela.TaskControl

Example of the process:

Quick start

using iszbela.TaskControl;
using System;
using System.Threading;

public class Example
{
    private static readonly TaskControl taskcontrol = new TaskControl();

    public void Test()
    {
        using var cts = new CancellationTokenSource();

        
        taskcontrol.AddAsync(() => Console.WriteLine("Async 1"), cts.Token);
        taskcontrol.AddAsync(() => Console.WriteLine("Async 2"), cts.Token);
        taskcontrol.AddAsync(() => Console.WriteLine("Async 3"), cts.Token);
        taskcontrol.AddAsync(() => Console.WriteLine("Async 4"), cts.Token);

        taskcontrol.AddSync(() => Console.WriteLine("Sync 1"), cts.Token);

        taskcontrol.AddAsync(() => Console.WriteLine("Async 5"), cts.Token);
        taskcontrol.AddAsync(() => Console.WriteLine("Async 6"), cts.Token);
        taskcontrol.AddAsync(() => Console.WriteLine("Async 7"), cts.Token);

        taskcontrol.AddSync(() => Console.WriteLine("Sync 2"), cts.Token);
        taskcontrol.AddSync(() => Console.WriteLine("Sync 3"), cts.Token);
        taskcontrol.AddSync(() => Console.WriteLine("Sync 4"), cts.Token);

        taskcontrol.AddAsync(() => Console.WriteLine("Async 8"), cts.Token);
        taskcontrol.AddAsync(() => Console.WriteLine("Async 9"), cts.Token);

        taskcontrol.AddSync(() => Console.WriteLine("Sync 5"), cts.Token);
    }
}
flowchart TD
    R1[[Async 2]] & R2[[Async 2]] & R3[[Async 3]]  & R4[[Async 4]] --> W1[[Sync 1]] --> R5[[Async 5]] & R6[[Async 6]] & R7[[Async 7]] --> W2[[Sync 2]] --> W3[[Sync 3]] --> W4[[Sync 4]] --> R8[[Async 8]] & R9[[Async 9]] --> W5[[Sync 5]]
    classDef r fill:#e8f5e9,stroke:#2e7d32,color:#1b5e20;
    classDef w fill:#fff3e0,stroke:#ef6c00,color:#e65100,font-weight:bold;
    class R1,R2,R3,R4,R5,R6,R7,R8,R9 r;
    class W1,W2,W3,W4,W5 w;

This makes it easy to make non-thread-safe processes thread-safe. Use it for any procedure that is not thread-safe but needs to be accessed from multiple threads simultaneously. The only thing you need to consider is which operations can run concurrently and which ones must execute sequentially (i.e., synchronously). You don't need to handle the implementation of the control mechanism.

using iszbela.TaskControl;
using System;
using System.Collections.Generic;
using System.Threading;

public class Example
{
    private static readonly List<int> ints = new();
    private static readonly TaskControl taskcontrol = new();

    public void Test()
    {
        using var cts = new CancellationTokenSource();

        // Async readers (can overlap)
        taskcontrol.AddAsync(() =>
        {
            foreach (var ii in ints)
            {
                // read-only work...
            }
        }, cts.Token);

        taskcontrol.AddAsync(() =>
        {
            foreach (var ii in ints)
            {
                // read-only work...
            }
        }, cts.Token);

        // A sync writer acts as a barrier => no overlap with readers/writers
        taskcontrol.AddSync(() =>
        {
            if (ints.Count > 2) ints.RemoveAt(2);
        }, cts.Token);

        // Readers after the writer (may overlap with each other again)
        taskcontrol.AddAsync(() =>
        {
            foreach (var ii in ints)
            {
                // read-only work...
            }
        }, cts.Token);
    }
}

Multi‑producer enqueue (threads adding work)

using iszbela.TaskControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
    private readonly List<int> ints = new() { 0,1,2,3,34,34,55,2,2 };
    private static readonly TaskControl taskcontrol = new();

    public async Task Test()
    {
        using var cts = new CancellationTokenSource();

        // Multiple threads enqueue tasks concurrently
        Task.Run(() => taskcontrol.AddAsync(() =>
        {
            foreach (var item in ints) Console.WriteLine(item);
        }, cts.Token));

        Task.Run(() => taskcontrol.AddAsync(() =>
        {
            foreach (var item in ints) Console.WriteLine(item);
        }, cts.Token));

        Task.Run(() => taskcontrol.AddSync(() =>
        {
            if (ints.Count > 0) ints.RemoveAt(0);
        }, cts.Token));

        // Awaitable async task variant that returns a result (as in the screenshot)
        List<int> ii = null!;
        await taskcontrol.AddAsyncTask(() =>
        {
            ii = ints.Where(x => x == 2).ToList();
        }, cts.Token);

        Console.WriteLine($"ii list count: {ii.Count}");
    }
}

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 was computed. 
.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.
  • .NETStandard 2.0

    • No dependencies.

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.2 200 8/17/2025