Created
September 27, 2018 10:22
-
-
Save nitinjs/52dc9a689332c5440e47ebcd2b2c7926 to your computer and use it in GitHub Desktop.
Processing large number of items in parallel
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 Parallel library dataflow can be obtained by nuget: https://www.nuget.org/packages/System.Threading.Tasks.Dataflow/ | |
var watch = new System.Diagnostics.Stopwatch(); | |
watch.Start(); | |
Action<int> an = n => { | |
//add your processing logic here | |
Console.WriteLine(n); | |
}; | |
var options = new ExecutionDataflowBlockOptions | |
{ | |
MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded | |
}; | |
var actionBlock = new ActionBlock<int>(an, options); | |
Parallel.ForEach(Enumerable.Range(1, 100), (x) => { | |
actionBlock.Post(x); | |
}); | |
actionBlock.Complete(); | |
actionBlock.Completion.Wait(); | |
watch.Stop(); | |
Console.WriteLine($"Time take to execute: {watch.ElapsedMilliseconds} ms"); | |
Console.ReadLine(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment