Created
June 17, 2022 15:36
-
-
Save BluePositive/85a8a730339a06b50fabcab6df9d8e7d to your computer and use it in GitHub Desktop.
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
public static Task ParallelForEachAsync<T>( | |
IEnumerable<T> source, | |
int degreeOfParallelization, | |
Func<T, Task> body) | |
{ | |
async Task AwaitPartition(IEnumerator<T> partition) | |
{ | |
using (partition) | |
{ | |
while (partition.MoveNext()) | |
{ | |
await body(partition.Current).ConfigureAwait(false); | |
} | |
} | |
} | |
return Task.WhenAll( | |
Partitioner | |
.Create(source) | |
.GetPartitions(degreeOfParallelization) | |
.AsParallel() | |
.Select(AwaitPartition)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment