Created
September 27, 2018 10:39
-
-
Save markvincze/58c9707891870b53aeabe13f9647d995 to your computer and use it in GitHub Desktop.
Async parallel processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var runningTasks = new ConcurrentDictionary<Task, string>(); | |
var maxDegreeOfParallelism = 5; | |
var semaphore = new SemaphoreSlim(maxDegreeOfParallelism); | |
foreach (var job in jobs) | |
{ | |
await semaphore.WaitAsync(); | |
var task = ProcessAsync(job); | |
runningTasks.TryAdd(task, ""); | |
await task.ContinueWith(t => | |
{ | |
runningTasks.TryRemove(t, out _); | |
semaphore.Release(); | |
}); | |
} | |
await Task.WhenAll(runningTasks.Keys.ToArray()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment