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…

  1. 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)
        {        
    
        }
    }
  2. 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} seconds");
    Parallel.Invoke(
        () => PerformSomeTask(3),
        () => PerformSomeTask(5),
        () => PerformSomeTask(2)
        );
                
    WriteLine($"Parallel.Invoke completed at {DateTime.Now.Second} seconds");
  3. In the PerformSomeTask() method, make the thread sleep for the amount of seconds passed to the method as the parameter (converting the seconds to milliseconds by multiplying it by 1000):
    int threadID = Thread.CurrentThread.ManagedThreadId;
    WriteLine($"Sleep thread {threadID} for {sleepSeconds} seconds");
    Thread.Sleep(sleepSeconds * 1000);
    WriteLine($"Thread {threadID} resumed");
  4. When you have added all the code, your Recipes class should look like this:
    public class Recipes
    {
        public void ParallelInvoke()
        {
            WriteLine($"Parallel.Invoke started at {DateTime.Now.Second} seconds");
            Parallel.Invoke(
                () => PerformSomeTask(3),
                () => PerformSomeTask(5),
                () => PerformSomeTask(2)
                );
                
            WriteLine($"Parallel.Invoke completed at {DateTime.Now.Second} seconds");           
        }
    
        private void PerformSomeTask(int sleepSeconds)
        {        
            int threadID = Thread.CurrentThread.ManagedThreadId;
            WriteLine($"Sleep thread {threadID} for {sleepSeconds} seconds");
            Thread.Sleep(sleepSeconds * 1000);
            WriteLine($"Thread {threadID} resumed");
        }
    }
  5. In the console application, instantiate a new instance of the Recipes class and call the ParallelInvoke() method:
    Chapter7.Recipes oRecipe = new Chapter7.Recipes();
    oRecipe.ParallelInvoke();
    Console.ReadLine();
  6. Run the console application and look at the output produced in the console window:
    How to do it…

How it works…

Because we are running all these threads in parallel, we can assume that the longest process will denote the total duration of the all the tasks. This means that the total duration of the process will be 5 seconds because the longest task will take 5 seconds to complete (we set thread 10 to sleep for a maximum of 5 seconds).

As we can see, the time difference between the start and the end of Parallel.Invoke is exactly 5 seconds.

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

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