Break parallel.foreach?

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

    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:

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

提交回复
热议问题