Break parallel.foreach?

前端 未结 5 618
醉酒成梦
醉酒成梦 2020-11-30 00:25

How do I break out of an parallel.for loop?

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

Parallel.ForEach

        
5条回答
  •  遥遥无期
    2020-11-30 01:00

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

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

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

提交回复
热议问题