连续.NET并行​​对于的 - 之后的每个阻止NET

2023-09-03 16:52:29 作者:容易受伤的孩子

我有连续几个的Parallel.For操作。

I have several Parallel.For operations in succession.

我目前正在研究每一个的Parallel.For的返回值 ParallelLoo presult 和沉睡了20毫秒,直到 IsCompleted 成员设置为true。

I am currently examining each Parallel.For's return value ParallelLoopResult and sleeping for 20 milliseconds until the IsCompleted member is set to true.


Dim plr as ParallelLoopResult 

plr = Parallel.For(...)

while not plr.IsCompleted
  Thread.Sleep(20)
end while

plr = Parallel.For(...)

while not plr.IsCompleted
  Thread.Sleep(20)
end while

.
.
.

我怎样才能到位环和Thread.sleep代码中增加一个内核级块(即WaitHandle的)?是否有一个完成事件的Par​​allel.For触发?有没有的Parallel.For提供这样的机制?

How can I add a kernel level block (i.e. WaitHandle) in place of the loop and Thread.Sleep? Is there a completion event that Parallel.For triggers? Does Parallel.For provide for such a mechanism?

推荐答案

的Parallel.For 将完成,这是要求所有code。该 IsCompleted 只返回则循环被打断了。

The Parallel.For will complete all code that it was called for. The IsCompleted only returns false then the loop was interrupted.

从的http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloo$p$psult.aspx:

如果IsCompleted返回true,则循环运行完成,使得循环的所有迭代被处决。如果IsCompleted返回false和LowestBreakIteration返回null,则调用停止用于pmaturely结束循环$ P $。如果IsCompleted返回false和LowestBreakIteration返回一个非空的整数值,歇用来pmaturely结束循环$ P $。

If IsCompleted returns true, then the loop ran to completion, such that all iterations of the loop were executed. If IsCompleted returns false and LowestBreakIteration returns null, a call to Stop was used to end the loop prematurely. If IsCompleted returns false and LowestBreakIteration returns a non-null integral value, Break was used to end the loop prematurely.