C# 打破parallel.foreach?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12571048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:44:45  来源:igfitidea点击:

Break parallel.foreach?

c#multithreadingparallel-processingparallel.foreach

提问by Rasmus S?borg

How do I break out of an parallel.forloop?

如何跳出parallel.for循环?

I have a pretty complex statement which looks like the following:

我有一个非常复杂的语句,如下所示:

Parallel.ForEach<ColorIndexHolder>(ColorIndex.AsEnumerable(),
    new Action<ColorIndexHolder>((ColorIndexHolder Element) =>
    {
        if (Element.StartIndex <= I && Element.StartIndex + Element.Length >= I)
        {
            Found = true;
            break;
        }
    }));

Using parallel class, I can optimize this process by far. However; I cannot figure out how to break the parallel loop? The break;statement throws following syntax error:

使用并行类,我可以优化这个过程。然而; 我不知道如何打破并行循环?该break;语句抛出以下语法错误:

No enclosing loops out of which to break or continue

没有可以中断或继续的封闭循环

采纳答案by Tudor

Use the ParallelLoopState.Breakmethod:

使用ParallelLoopState.Break方法:

 Parallel.ForEach(list,
    (i, state) =>
    {
       state.Break();
    });

Or in your case:

或者在你的情况下:

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();
        }
    }));

回答by Reed Copsey

You do this by calling using the overload of Parallel.Foror Parallel.ForEachwhich passes in a loop state, then calling ParallelLoopState.Breakor ParallelLoopState.Stop. The main difference is in how quickly things break - with Break(), the loop will process all items with an earlier "index" than the current. With Stop(), it will exit as quickly as possible.

您可以通过使用Parallel.FororParallel.ForEach以循环状态传递的重载进行调用,然后调用ParallelLoopState.Breakor 来完成此操作ParallelLoopState.Stop。主要区别在于事情中断的速度 - 使用Break(),循环将处理具有比当前更早“索引”的所有项目。使用Stop(),它将尽快退出。

For details, see How to: Stop or Break from a Parallel.For Loop.

有关详细信息,请参见如何:停止或中断 Parallel.For 循环

回答by Mike Perrenoud

Just use the loopStatethat can be provided.

只需使用loopState可以提供的。

Parallel.ForEach<ColorIndexHolder>(ColorIndex.AsEnumerable(),  
    new Action<ColorIndexHolder>((Element, loopState) => { 
        if (Element.StartIndex <= I && Element.StartIndex + Element.Length >= I) { 
            loopState.Stop();
        }     
})); 

Look at this MSDN articlefor an example.

以这篇MSDN 文章为例。

回答by Servy

What you should be using is Any, rather than a foreach loop:

您应该使用的是Any,而不是 foreach 循环:

bool Found = ColorIndex.AsEnumerable().AsParallel()
    .Any(Element => Element.StartIndex <= I 
      && Element.StartIndex + Element.Length >= I);

Anyis smart enough to stop as soon as it knows that the result must be true.

Any它足够聪明,一旦知道结果必须是真的就停止。

回答by MBentley

LoopState is certainly a great answer. I found the previous answers had so much other stuff that it was hard to see the answer, so here is a simple case:

LoopState 无疑是一个很好的答案。我发现以前的答案有很多其他的东西,很难看到答案,所以这里是一个简单的案例:

using System.Threading.Tasks;

Parallel.ForEach(SomeTable.Rows(), (row, loopState) =>
{
    if (row.Value == testValue)
    {
        loopState.Stop();  // Stop the ForEach!
    }       
    // else do some other stuff here.
});