[英]How to make each method wait to complete another inside of parallel.Foreach?
我通過使用“任務並行”對“並行”有一個大問題。 我想使我的方法和函數同步(彼此等待)。
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...
});
我怎樣才能做到這一點?
Parallel.For
將在您的items
集合中Parallel.For
運行。 它處理的每個item
都將同步調用給定的委托。 意思是,它將先執行MyClass1.Function
然后MyClass2.Function
然后MyClass2.Method1
。 這是假設您的方法是同步的,並且您在其中的任何后台線程中均未執行任何操作。
像這樣想象:
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.