RuriLib.Parallelization 2.0.0

dotnet add package RuriLib.Parallelization --version 2.0.0
                    
NuGet\Install-Package RuriLib.Parallelization -Version 2.0.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="RuriLib.Parallelization" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RuriLib.Parallelization" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RuriLib.Parallelization" />
                    
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 RuriLib.Parallelization --version 2.0.0
                    
#r "nuget: RuriLib.Parallelization, 2.0.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.
#:package RuriLib.Parallelization@2.0.0
                    
#: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=RuriLib.Parallelization&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RuriLib.Parallelization&version=2.0.0
                    
Install as a Cake Tool

RuriLib.Parallelization

This is a library that can perform multiple tasks (yes, a lot, even infinitely many) that act on some input and return a certain output.

Features:

  • Fully asynchronous
  • Dynamic degree of parallelism (change it while it runs)
  • Pausing and resuming
  • Soft stop and hard abort
  • Automatic CPM calculation
  • Cancellation tokens for start, wait, pause, resume, stop, abort and DoP changes
  • Events

Requirements

Version 2.x targets .NET 10.

Installation

NuGet: dotnet add package RuriLib.Parallelization

Example

using RuriLib.Parallelization;
using RuriLib.Parallelization.Models;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ParallelizationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            _ = MainAsync(args);
            Console.ReadLine();
        }

        static async Task MainAsync(string[] args)
        {
            // This func takes an input type of 'int', a cancellation token, and an output type of `Task` of `bool`
            Func<int, CancellationToken, Task<bool>> parityCheck = new(async (number, token) => 
            {
                // This is the body of your work function
                await Task.Delay(50, token);
                return number % 2 == 0;
            });

            var parallelizer = ParallelizerFactory<int, bool>.Create(
                type: ParallelizerType.TaskBased, // Use task-based (it's better)
                workItems: Enumerable.Range(1, 100), // The work items are all integers from 1 to 100
                workFunction: parityCheck, // Use the work function we defined above
                degreeOfParallelism: 5, // Use 5 concurrent tasks at most
                totalAmount: 100, // The total amount of tasks you expect to have, used for calculating progress
                skip: 0); // How many items to skip from the start of the provided enumerable

            // Hook the events
            parallelizer.NewResult += OnResult;
            parallelizer.Completed += OnCompleted;
            parallelizer.Error += OnException;
            parallelizer.TaskError += OnTaskError;

            await parallelizer.Start();

            // It's important to always pass a cancellation token to avoid waiting forever if something goes wrong!
            var cts = new CancellationTokenSource();
            cts.CancelAfter(10000);

            await parallelizer.WaitCompletion(cts.Token);
        }

        private static void OnResult(object sender, ResultDetails<int, bool> value)
            => Console.WriteLine($"Got result {value.Result} from the parity check of {value.Item}");
        private static void OnCompleted(object sender, EventArgs e) => Console.WriteLine("All work completed!");
        private static void OnTaskError(object sender, ErrorDetails<int> details)
            => Console.WriteLine($"Got error {details.Exception.Message} while processing the item {details.Item}");
        private static void OnException(object sender, Exception ex) => Console.WriteLine($"Exception: {ex.Message}");
    }
}

To change the degree of parallelism while it's running, for example to speed up or slow down the work, you can write

await parallelizer.ChangeDegreeOfParallelism(10, cts.Token);

You can also pause and resume work. Notice that pausing will wait until all the tasks that are being worked on will end, so it will not have immediate action since there is no support for pausing tasks half-way.

await parallelizer.Pause(cts.Token);
// Do something
await parallelizer.Resume(cts.Token);

Finally, there are two ways to stop the parallelizer. You can either stop it (which, like pause, waits until all current tasks have ended) or abort it, which will cancel the cancellation token passed to the tasks. You should constantly check if the cancellation token has been cancelled inside your work function.

await parallelizer.Stop(cts.Token);
await parallelizer.Abort(cts.Token);

You can check how fast the parallelizer is processing items or how much time is remaining by accessing the corresponding properties

Console.WriteLine($"Doing {parallelizer.CPM} checks per minute and the remaining time is {parallelizer.Remaining}");

Changelog

See CHANGELOG.md for release notes.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.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
2.0.0 55 6/6/2026
1.0.6 2,176 3/9/2022
1.0.5 1,520 1/9/2022
1.0.4 530 1/9/2022
1.0.3 737 4/8/2021
1.0.2 635 3/16/2021
1.0.1 593 3/1/2021
1.0.0 643 3/1/2021