Running Tasks with Parallel.Invoke

The first way for running tasks in parallel is calling the Parallel.Invoke shared method. This method can receive an array of System.Action objects as parameter, so each Action is translated by the runtime into a task. If possible, tasks are executed in parallel. The following example demonstrates how to perform three calculations concurrently:

image

In the example the code takes advantage of statement lambdas; each of them is translated into a task by the runtime that is also responsible for creating and scheduling threads and for scaling tasks across all available processors. If you run the code you can see how the tasks run within separate threads, automatically created for you by the TPL. As an alternative you can supply AddressOf clauses pointing to methods performing the required operations, instead of using statement lambdas. Although this approach is useful when you need to run tasks in parallel the fastest way, it does not enable you to take control over tasks themselves. This is instead something that requires explicit instances of the Task class, as explained in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.181.166