且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 C# 中并行运行三种方法的最简单方法

更新时间:2023-11-18 08:04:40

请参阅 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.