-
Book Overview & Buying
-
Table Of Contents
C# Programming Cookbook
By :
Parallel.Invoke allows us to execute tasks in (you guessed it) parallel. Sometimes, you need to perform operations simultaneously and, in so doing, speed up processing. You can therefore expect that the total time taken to process the tasks is equal to the longest running process. Using Parallel.Invoke is quite easy.
Make sure that you have added the using System.Threading.Tasks; statement to the top of your Recipes class.
Start off by creating two methods in the Recipes class called ParallelInvoke() and PerformSomeTask(), which take an integer of seconds to sleep as the parameter:
public class Recipes
{
public void ParallelInvoke()
{
}
private void PerformSomeTask(int sleepSeconds)
{
}
}Add the following code to the ParallelInvoke() method. This code will call Paralell.Invoke to run the PerformSomeTask() method:
WriteLine($"Parallel.Invoke started at {DateTime.Now.Second...