我如何突破 parallel.for循环?
我有一个非常复杂的语句,如下所示:
Parallel.ForEach<ColorIndexHolder>(ColorIndex.AsEnumerable(),
new Action<ColorIndexHolder>((ColorIndexHolder Element) =>
{
if (Element.StartIndex <= I && Element.StartIndex + Element.Length >= I)
{
Found = true;
break;
}
}));
使用并行类,我可以优化这个过程。然而;我不知道如何打破并行循环? break;
语句抛出以下语法错误:
No enclosing loops out of which to break or continue
最佳答案
使用 ParallelLoopState.Break
方法:
Parallel.ForEach(list,
(i, state) =>
{
state.Break();
});
或者在你的情况下:
Parallel.ForEach<ColorIndexHolder>(ColorIndex.AsEnumerable(),
new Action<ColorIndexHolder, ParallelLoopState>((ColorIndexHolder Element, ParallelLoopState state) =>
{
if (Element.StartIndex <= I && Element.StartIndex + Element.Length >= I)
{
Found = true;
state.Break();
}
}));
关于c# - 打破 parallel.foreach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12571048/