I have the following code inside a bigger loop, after profiling my c开发者_如何学编程ode I discovered that all the Parallel.For gain in execution speed is lost in the long time the Stop()
method takes to complete. Is there any way to improve this? Maybe calling Thread.Sleep()
?
Thanks.
Parallel.For(0, 1000, (i, loopState) =>
{
if (a == b)
loopState.Stop();
});
I think you should use loopState.Break()
method, since it is paralel for break
keyword. The Stop
method sets IsStopped
flag, so that other iterations may check this flag and stop at their convenience. It does not stop the loop.
See Stop and Break on msdn
精彩评论