How can I wait till the Parallel.ForEach completes

VJAI picture VJAI · Oct 25, 2011 · Viewed 94.4k times · Source

I'm using TPL in my current project and using Parallel.Foreach to spin many threads. The Task class contains Wait() to wait till the task gets completed. Like that, how I can wait for the Parallel.ForEach to complete and then go into executing next statements?

Answer

Henk Holterman picture Henk Holterman · Oct 25, 2011

You don't have to do anything special, Parallel.Foreach() will wait until all its branched tasks are complete. From the calling thread you can treat it as a single synchronous statement and for instance wrap it inside a try/catch.