Skip to content

GuildOfCalamity/ConcurrentUtils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C# Concurrent Utilities


I have ported this entire project and made it usable from the old xproj format. I have made this a multi-framework project (.NET 4.8.1 & .NET 6.0) and also applied fixes and improvements where needed.

The original can be found here.


Provides classes and methods useful in concurrent programming.

Functionalities

Times(long times, int limit, Func<long, Task> method)

Executes an asynchronous method n number of times, limiting the amount of operations in parallel without blocking.

Returns a Task that is completed when all Tasks are completed or is faulted when any of the Tasks transition to faulted state.

Suitable for benchmarking asynchronous methods with different maximum amount of parallel operations.

Example

// Execute MyMethodAsync 1,000,000 times limiting the maximum amount of parallel async operations to 512
await ConcurrentUtils.Times(1000000, 512, (index) => MyMethodAsync());

Map(IList<TSource> source, int limit, Func<TSource, Task<TResult>> method)

Asynchronously projects each element of a sequence into a new form, limiting the amount of operations in parallel without blocking.

Returns a Task that gets completed with the transformed elements or faulted when any of the transformation operations transition to faulted state.

Example

var urls = new []
{
    "https://www.google.com/",
    "https://www.microsoft.com/net/core",
    "https://www.nuget.org/",
    "https://dotnet.github.io/"
};
// Asynchronously get the http response of each url limiting
// the maximum amount of parallel http requests to 2
string[] responses = await ConcurrentUtils.Map(urls, 2, url => client.GetStringAsync(url));

CreateQueue<T>(int limit, Func<T, Task> method)

Creates collection of objects to which apply the asynchronous method in a first-in first-out manner. Items added to the queue are processed in parallel according to the given limit.

Returns a IJobQueue<T> instance that can be used to enqueue items.

Example

// Create the queue providing the method that is going to be used to asynchronously process each item
// and the max amount of parallel operations (in this case 2)
IJobQueue<string> jobQueue = ConcurrentUtils.CreateQueue(2, url => client.GetStringAsync(url));
// Add items to the queue that are going to be processed
Task t1 = jobQueue.Enqueue("https://www.google.com/");
Task t2 = jobQueue.Enqueue("https://www.microsoft.com/net/core");
Task t3 = jobQueue.Enqueue("https://www.nuget.org/");
Task t4 = jobQueue.Enqueue("https://dotnet.github.io/");
// Items are processed as FIFO queue, without exceeding the max amount of parallel operations limit
await Task.WhenAll(t1, t2, t3, t4);

console

About

Concurrent utility library for Task

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages