Created
May 11, 2017 15:55
-
-
Save AArnott/0daef1cba10f79150a9fc1d5707beb58 to your computer and use it in GitHub Desktop.
Sample of a method that throttles concurrent work to avoid flooding the threadpool queue
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
Task ThrottleParallelWorkAsync(IEnumerable<T> data, Action<T> worker) | |
{ | |
TaskScheduler scheduler = new ConcurrentExclusiveSchedulerPair( | |
TaskScheduler.Default, // schedule work to the ThreadPool | |
Environment.ProcessorCount * 2) // Schedule enough to keep all threads busy, with a queue to quickly replace completed work | |
.ConcurrentScheduler; // We only use the concurrent member of this scheduler "pair". | |
return Task.WhenAll( | |
data.Select(v => Task.Factory.StartNew(() => worker(v), CancellationToken.None, TaskCreationOptions.None, scheduler))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment