Skip to content

Latest commit

 

History

History
55 lines (38 loc) · 1.71 KB

File metadata and controls

55 lines (38 loc) · 1.71 KB

ConcurrentTask

NuGet License

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.

Usage

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.

Contributing

Please read CONTRIBUTING.md for details on how to contribute to this project.

Acknowledgements

Borrowed from the excellent SafeParallelAsync ❤️

License

ConcurrentTask is released under the MIT License