简体   繁体   English

如何使每个方法等待在parallel.Foreach内部完成另一个方法?

[英]How to make each method wait to complete another inside of parallel.Foreach?

I have a big problem for "parallel For" by using Task Parallel. 我通过使用“任务并行”对“并行”有一个大问题。 I want to make my methods and functions synchronous (wait on each other). 我想使我的方法和函数同步(彼此等待)。

Parallel.For(items, item=>
{
    var a = MyClass1.Function(foo.x);
    var b = MyClass2.Function(zoo.y, b.z);  ---> Should wait "a" result...
    var c = MyClass2.Method1(a.x,b.z); -----> Should wait b result...
});  

How can I do that? 我怎样才能做到这一点?

Parallel.For will run in parallel across your collection of items . Parallel.For将在您的items集合中Parallel.For运行。 Each item it processes, will invoke the given delegate synchronously. 它处理的每个item都将同步调用给定的委托。 Meaning, it will execute MyClass1.Function then MyClass2.Function then MyClass2.Method1 . 意思是,它将先执行MyClass1.Function然后MyClass2.Function然后MyClass2.Method1 This is assuming your methods are synchronous, and you're not doing anything in background threads inside any of them. 这是假设您的方法是同步的,并且您在其中的任何后台线程中均未执行任何操作。

Imagine it like this: 像这样想象:

                Items
  |          |          |          |
 Item1:      Item2:     Item3:     Item4:
Function1  Function1  Function1  Function1
Function2  Function2  Function2  Function2
Method1    Method1    Method1    Method1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM