Why use concurrent tasks?
Typically, Parallel.ForEach is used for running parallel tasks that are CPU-bound. There is a chance that using
the same method for I/O-bound operations can lead to port exhaustion, such as for RESTful calls or web methods.
ConcurrentTask solves this problem by allowing async tasks to run concurrently, and allowing the consumer
to specify how many tasks to run at the same time.
Install the package from NuGet with dotnet add package ConcurrentTask.
You can run your tasks concurrently using the below:
var maxParallelTasks = 10;
var values = Enumerable.Range(1, 10);
await Concurrent.ForEachAsync(values, maxParallelTasks, async (i, token) =>
{
await Task.Yield();
});To return results with your tasks, use the below:
var values = Enumerable.Range(1, 10);
var results = Concurrent.ForEachWithResultAsync(values, async (i, token) =>
{
await Task.Yield();
return i;
});
await foreach (var result in results)
{
// do something
}By default, Environment.ProcessorCount is used to limit the number of concurrent tasks.
Please read CONTRIBUTING.md for details on how to contribute to this project.
Borrowed from the excellent SafeParallelAsync ❤️
ConcurrentTask is released under the MIT License