Codibre.ConcurrentTaskRunner 0.0.1

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

// Install Codibre.ConcurrentTaskRunner as a Cake Tool
#tool nuget:?package=Codibre.ConcurrentTaskRunner&version=0.0.1                

Actions Status Actions Status Actions Status Maintainability Test Coverage

Codibre.ConcurrentTaskRunner

A lib for concurrent, semaphore limited task runner under the hood

How to use?

Create an instance of options specifying the desired concurrency limit

ConcurrentTaskRunnerOptions options = new()
{
    Limit = 3
};

Then, crate your runner instance

ConcurrentTaskRunner runner = new(options);

Finally, delegate the calls you want to limit concurrency

await runner.Run(MyCall);

Notice that, if the method Run never waits for MyCall to finish, it actually waits for a run slot to be freed, that is, if the concurrency limit hasn't been reached yet, it'll not wait and just pass through to MyCall execution in background.

Error handling

By default, any error the callback passed to Run throws is ignored, but you can treat it somehow informing a callback in the options to deal with it, like this:

ConcurrentTaskRunnerOptions options = new()
{
    Limit = 3,
    OnError = (Exception err) => Console.WriteLine(err.Message)
}

Errors thrown by OnError callback are not ignored, though, so write it carefully making sure it'll not throw anything so you don't get an untreated exception.

Procressing an Enumerable or AsyncEnumerable

It's also possible to use ConcurrentTaskRunner to process in a concurrently limited pace an enumerable or async enumerable instance. You just need to use our extension method RunConcurrently, like this:

await enumerable
    .RunConcurrently(runner, (x) => MyCall(x));

Notice that a AsyncEnumerable may be infinite, so this is a blocking line in your code if that's the case. A CancellationToken instance can be used to create a stoppage condition:

await enumerable
    .RunConcurrently(runner, (x) => MyCall(x), cancellationToken);

In this implementation, cancellationToken will not throw an error, but before executing the callback, it'll be checked to validate whether the cancellation had been requested or not, and it's not propagated to the callback. If you need it, though, you can propagate it yourself, like this:

await enumerable
    .RunConcurrently(runner, (x) => MyCall(x, cancellationToken), cancellationToken);
Product Compatible and additional computed target framework versions.
.NET 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 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.  net9.0 is compatible. 
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
0.0.2 306 11/28/2024
0.0.1 80 11/28/2024