Skip to content

Instantly share code, notes, and snippets.

@AArnott
Created May 11, 2017 15:55
Show Gist options
  • Save AArnott/0daef1cba10f79150a9fc1d5707beb58 to your computer and use it in GitHub Desktop.
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
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