Created
January 6, 2020 13:27
-
-
Save scattered-code/b834bbc355a9ee710e3147321d6f985a 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 async Task AsyncParallelForEach<T>(this IAsyncEnumerable<T> source, Func<T, Task> body, int maxDegreeOfParallelism = DataflowBlockOptions.Unbounded, TaskScheduler scheduler = null) | |
{ | |
var options = new ExecutionDataflowBlockOptions | |
{ | |
MaxDegreeOfParallelism = maxDegreeOfParallelism | |
}; | |
if (scheduler != null) | |
options.TaskScheduler = scheduler; | |
var block = new ActionBlock<T>(body, options); | |
await foreach (var item in source) | |
block.Post(item); | |
block.Complete(); | |
await block.Completion; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment