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 the 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've added the using System.Threading.Tasks;
statement to the top of your Demo
class.
How to do it...
- Start off by creating two methods in the
Demo
class calledParallelInvoke()
andPerformSomeTask()
that take an integer of seconds to sleep as the parameter:
public class Demo { public void ParallelInvoke() { } private void PerformSomeTask(int sleepSeconds) { } }
- Add the following code to the
ParallelInvoke()
method. This code will callParalell.Invoke
to run thePerformSomeTask...