Simplest way to run three methods in C # at the same time

I have three methods that I call to execute some number of crunch, the following

results.LeftFront.CalcAi(); results.RightFront.CalcAi(); results.RearSuspension.CalcAi(geom, vehDef.Geometry.LTa.TaStiffness, vehDef.Geometry.RTa.TaStiffness); 

Each of the functions is independent of each other and can be calculated in parallel without dead locks.
What is the easiest way to compute them in parallel without completing the method until all three are completed?

+45
c # parallel-processing task-parallel-library
Sep 06 '11 at 13:13
source share
5 answers

See the TPL documentation. They list this example:

 Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork()); 

So, in your case, this should work:

 Parallel.Invoke( () => results.LeftFront.CalcAi(), () => results.RightFront.CalcAi(), () => results.RearSuspension.CalcAi(geom, vehDef.Geometry.LTa.TaStiffness, vehDef.Geometry.RTa.TaStiffness)); 

EDIT:. The call returns after completion of all actions. Invoke() does not guarantee that they will actually work in parallel, nor does it guarantee the order in which actions are performed.

+83
Sep 06 '11 at 13:18
source share

You can do this with tasks too (better if you need cancellation or something like results)

 var task1 = Task.Factory.StartNew(() => results.LeftFront.CalcAi()); var task2 = Task.Factory.StartNew(() => results.RightFront.CalcAi()); var task3 = Task.Factory.StartNew(() =>results.RearSuspension.CalcAi(geom, vehDef.Geometry.LTa.TaStiffness, vehDef.Geometry.RTa.TaStiffness)); Task.WaitAll(task1, task2, task3); 
+17
Sep 06 2018-11-11T00:
source share

In .NET 4, Microsoft introduced a parallel task library that was designed to solve this problem; see Parallel Programming in the .NET Framework .

+2
Sep 06 '11 at 13:20
source share

You can also use ThreadPool.QueueUserWorkItem to run parallel methods that are independent of each other. Here is an example method -

 public static void ExecuteParallel (params Action[] tasks) { // Initialize the reset events to keep track of completed threads ManualResetEvent [] resetEvents = new ManualResetEvent [tasks. Length]; // Launch each method in it own thread for (int i = 0; i < tasks . Length; i ++) { resetEvents [i ] = new ManualResetEvent (false ); ThreadPool .QueueUserWorkItem ( new WaitCallback ((object index) => { int taskIndex = ( int) index ; // Execute the method tasks [taskIndex ](); // Tell the calling thread that we're done resetEvents [taskIndex ]. Set(); }), i ); } // Wait for all threads to execute WaitHandle .WaitAll ( resetEvents); } 

More about this feature can be found here -
http://newapputil.blogspot.in/2016/03/running-parallel-tasks-using.html

+1
Mar 10 '16 at 10:35
source share
 var task1 = SomeLongRunningTask(); var task2 = SomeOtherLongRunningTask(); await Task.WhenAll(task1, task2); 

The advantage of this over Task.WaitAll is that it will free the thread and wait for two tasks to complete.

0
Jul 11 '16 at 5:45
source share



All Articles