C# 打破平行线,foreach?

C# 打破平行线,foreach?,c#,multithreading,parallel-processing,parallel.foreach,C#,Multithreading,Parallel Processing,Parallel.foreach,如何打破循环? 我有一个非常复杂的陈述,如下所示: Parallel.ForEach<ColorIndexHolder>(ColorIndex.AsEnumerable(), new Action<ColorIndexHolder>((ColorIndexHolder Element) => { if (Element.StartIndex <= I && Element.StartIndex + Element

如何打破循环?

我有一个非常复杂的陈述,如下所示:

Parallel.ForEach<ColorIndexHolder>(ColorIndex.AsEnumerable(),
    new Action<ColorIndexHolder>((ColorIndexHolder Element) =>
    {
        if (Element.StartIndex <= I && Element.StartIndex + Element.Length >= I)
        {
            Found = true;
            break;
        }
    }));
Parallel.ForEach(ColorIndex.AsEnumerable(),
新操作((ColorIndexHolder元素)=>
{
if(Element.StartIndex=I)
{
发现=真;
打破
}
}));
使用并行类,到目前为止我可以优化这个过程。然而;我不知道如何打破平行循环?
中断语句引发以下语法错误:

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


为此,您可以使用重载
Parallel.For
Parallel.ForEach
进行调用,该重载以循环状态传递,然后调用或。主要区别在于事物中断的速度有多快-使用
break()
,循环将处理所有“索引”早于当前索引的项。使用
Stop()
,它将尽快退出

有关详细信息,请参阅。

使用以下方法:

或者在您的情况下:

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();
        }
    }));
Parallel.ForEach(ColorIndex.AsEnumerable(),
新操作((ColorIndexHolder元素,ParallelLoopState状态)=>
{
if(Element.StartIndex=I)
{
发现=真;
state.Break();
}
}));

只需使用可以提供的
循环状态即可

Parallel.ForEach<ColorIndexHolder>(ColorIndex.AsEnumerable(),  
    new Action<ColorIndexHolder>((Element, loopState) => { 
        if (Element.StartIndex <= I && Element.StartIndex + Element.Length >= I) { 
            loopState.Stop();
        }     
})); 
Parallel.ForEach(ColorIndex.AsEnumerable(),
新操作((元素,循环状态)=>{
如果(Element.StartIndex=I){
loopState.Stop();
}     
})); 

以此为例。

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

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

任何
都非常聪明,只要知道结果一定是真的,就可以立即停止。

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

您是否希望循环的所有并行实例同时中断?完全正确。考虑到一个连续的foreach循环,它保证了无论出于何种原因导致中断的项之前的项都会被处理。那么Parallel.ForEach呢,其中项目的顺序不一定是处理它们的顺序?是否还可以保证IEnumerable中调用state.Break()的项之前的所有项都在处理中,而后面的项则没有?虽然前者可以通过某种方式实现,但我不认为后者是可能的。@Hendrik Wiese:Docs说:
调用Break方法会通知for操作当前方法之后的迭代不必执行。但是,如果当前迭代之前的所有迭代尚未执行,则仍将必须执行。
不能保证当前迭代之后的迭代肯定不会执行。
因此,
state.Stop()
更适合可靠地实现预期结果,正如Mike Perrenoud和MBentleyIs在下面提到的,有没有一个比state?+1更直观的变量名,看起来我们中的一些人有完全相同的答案:)--哦,我得到了你对另一个评论人的支持。谢谢你的解释。您是否知道何时调用break或stop,是当前执行的迭代已完成,还是在执行中途停止迭代?@CeejeeB当前正在执行的操作已完成。
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.
});