How can I wait for Parallel.ForEach to complete

I am using TPL in my current project and using Parallel.Foreach to rotate many threads. The Task class contains Wait () to wait for the task to complete. How is it, how can I wait for Parallel.ForEach to complete, and then proceed to execute the following statements?

+73
c # task-parallel-library
Oct. 25 '11 at 9:11
source share
2 answers

You do not need to do anything, Parallel.Foreach() will wait for the completion of all its forked tasks. From the calling thread, you can consider it as one synchronous statement and, for example, wrap it inside try / catch.

+123
Oct 25 '11 at 9:13
source share

You do not need to do so with Parallel.Foreach: it only executes foreach as many threads as there are processors available, but it returns synchronously.

More information can be found here.

+9
Oct 25 '11 at 9:15
source share



All Articles