Break parallel.foreach?

前端 未结 5 619
醉酒成梦
醉酒成梦 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 00:47

    Just use the loopState that can be provided.

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

    Look at this MSDN article for an example.

提交回复
热议问题