简体   繁体   中英

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 . Each item it processes, will invoke the given delegate synchronously. Meaning, it will execute MyClass1.Function then MyClass2.Function then 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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