Break parallel.foreach?

前端 未结 5 625
醉酒成梦
醉酒成梦 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:36

    Use the ParallelLoopState.Break method:

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

    Or in your case:

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

提交回复
热议问题