I have three methods that I call to do some number crunching that are as follows
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 computed in parallel with no dead locks.
What is the easiest way to compute these in parallel without the containing method finishing until all three are done?
The benefit of this over Task.WaitAll is that this will release the thread and await the completion of the two tasks.
In .NET 4, Microsoft introduced the Task Parallel Library which was designed to handle this kind of problem, see Parallel Programming in the .NET Framework.
See the TPL documentation. They list this sample:
So in your case this should just work:
EDIT: The call returns after all actions have finished executing.
Invoke()
is does not guarantee that they will indeed run in parallel, nor does it guarantee the order in which the actions execute.To run parallel methods which are independent of each other ThreadPool.QueueUserWorkItem can also be used. Here is the sample method-
More detail about this function can be found here:
http://newapputil.blogspot.in/2016/03/running-parallel-tasks-using.html
You can do this with tasks too (nicer if you later need Cancellation or something like results)