A util that runs multiple async tasks concurrently with features for limiting and retry
$ dotnet add package Soenneker.ConcurrentProcessing.Executor
Soenneker.ConcurrentProcessing.ExecutorThis executor efficiently handles multiple tasks with controlled concurrency. It is ideal for managing parallel execution of tasks while ensuring that no more than a specified number of tasks run simultaneously.
⚠️ Note:
dotnet add package Soenneker.ConcurrentProcessing.Executor
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Soenneker.ConcurrentProcessing.Executor;
public class Program
{
public static async Task Main(string[] args)
{
var executor = new ConcurrentProcessingExecutor(maxConcurrency: 3);
var tasks = new List<Func<CancellationToken, ValueTask>>
{
async (ct) => {
Console.WriteLine("Task 1 started");
await Task.Delay(500, ct);
Console.WriteLine("Task 1 completed");
},
async (ct) => {
Console.WriteLine("Task 2 started");
await Task.Delay(300, ct);
Console.WriteLine("Task 2 completed");
},
async (ct) => {
Console.WriteLine("Task 3 started");
await Task.Delay(700, ct);
Console.WriteLine("Task 3 completed");
},
async (ct) => {
Console.WriteLine("Task 4 started");
await Task.Delay(400, ct);
Console.WriteLine("Task 4 completed");
}
};
await executor.Execute(tasks);
}
}Task 1 started
Task 2 started
Task 3 started
Task 1 completed
Task 4 started
Task 2 completed
Task 3 completed
Task 4 completed