同时运行三种方法在C#中最简单的方法 [英] Simplest way to run three methods in parallel in C#

查看:182
本文介绍了同时运行三种方法在C#中最简单的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我打电话做一些数字运算的三种方法如下:

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);

每个的功能是彼此独立的,并且可以平行地无死锁来计算。< BR>
什么是计算这些并行而不包含方法整理,直到所有三个都做了最简单的方法是什么?

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?

推荐答案

查看 TPL文档。他们列出此示例:

See the TPL documentation. They list this sample:

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



所以你的情况这应该只是工作:

So in your case this should just work:

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



编辑:所有动作后,调用返回已执行完毕。 的invoke() 是不保证它们确实将并行运行,也不能保证在该操作的执行顺序。

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.

这篇关于同时运行三种方法在C#中最简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆