Waiting for Multiple Tasks

The Parallel.Invoke method shown in the preceding section brings us to an interesting facility in the TPL. Did you see the one single most important difference compared to regular tasks? Here it is: Although the individual pieces of work passed to Parallel.Invoke can run in parallel using asynchronous tasks, the sum of the parts is synchronous. That is, Parallel.Invoke doesn’t return until all of its actions have been executed (or some error or cancellation has occurred).

If you read Chapter 29 carefully, you might immediately think of a way to achieve this kind of synchronizing behavior across multiple parallel actions. What about using events such as ManualResetEvent or even the concept of a CountDownEvent? Although ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.