Invoking parallel calls to methods using Parallel.Invoke
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.
Getting ready
Make sure that you have added the using System.Threading.Tasks; statement to the top of your Recipes class.
How to do it…
Start off by creating two methods in the
Recipesclass calledParallelInvoke()andPerformSomeTask(), 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 callParalell.Invoketo run thePerformSomeTask()method:WriteLine($"Parallel.Invoke started at {DateTime.Now.Second...