Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Async Governor - Divide and Conquer
// using System.Threading.Tasks
var maxIterations = 10000;
var maxDOP = 10;
// Divide into groups.
var parallelGroups = Enumerable.Range(0, maxIterations)
.GroupBy(r => (r % maxDOP));
var parallelTasks = parallelGroups.Select(groups =>
{
return Task.Run(async () =>
{
foreach (var i in groups)
{
// Do Async Stuff. (like IO)
// await as you please.
}
});
});
await Task.WhenAll(parallelTasks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment