138

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

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

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

Using parallel class, I can optimize this process by far. However; I cannot figure out how to break the parallel loop? The break; statement throws following syntax error:

No enclosing loops out of which to break or continue

1
  • 1
    Would you expect that ALL parallel instances of the loop break at the same time?
    – n8wrl
    Commented Sep 24, 2012 at 18:52

5 Answers 5

215

Use the ParallelLoopState.Break method:

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

Or in your case:

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();
        }
    }));
4
  • 1
    Thinking of a sequential foreach loop it's guaranteed that the items before the item that for whatever reason caused the break are processed. What about a Parallel.ForEach where order of the items does not necessarily have to be the order in which they are processed? Is it guaranteed as well that all items in an IEnumerable<...> before the one that invokes state.Break() are being processed and those coming after it are not? Although the former could be achieved somehow, I don't see how the latter would be possible at all. Commented Mar 14, 2013 at 12:29
  • 6
    @Hendrik Wiese: Docs say: Calling the Break method informs the for operation that iterations after the current one don't have to execute. However, all iterations before the current one will still have to be executed if they haven't already. and there is no guarantee that iterations after the current one will definitely not execute.
    – Tudor
    Commented Jun 27, 2014 at 9:19
  • 2
    so then would state.Stop() be more appropriate to reliably achieve the expected results, as mentioned below by Mike Perrenoud and MBentley
    – xtreampb
    Commented Sep 8, 2016 at 22:21
  • Is there a more intuitive variable name than state? Commented Dec 7, 2020 at 22:42
55

You do this by calling using the overload of Parallel.For or Parallel.ForEach which passes in a loop state, then calling ParallelLoopState.Break or ParallelLoopState.Stop. The main difference is in how quickly things break - with Break(), the loop will process all items with an earlier "index" than the current. With Stop(), it will exit as quickly as possible.

For details, see How to: Stop or Break from a Parallel.For Loop.

3
  • 3
    +1, it looks like a few of us here have the exact same answer :) -- oh and I got your back up there on that other comment man. Commented Sep 24, 2012 at 19:00
  • Thanks for this explanation. Do you know when either break or stop are called, is it the case that the currently executing iterations are completed or does it stop the iterations mid way through execution?
    – CeejeeB
    Commented May 7, 2013 at 7:33
  • 1
    @CeejeeB Currently executing operations complete. Commented May 10, 2013 at 15:09
19

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

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.

3
  • True. However, Any won't work if you have to perform some sort of other lookup or logic inside an iteration.
    – dst3p
    Commented Oct 21, 2022 at 17:50
  • 1
    @dst3p Why do you think it wouldn't work? Sure, if you're looking something up in some other data structure or doing other complex logic you need to be sure it's safe to do in a multithreaded context, but there are lots of ways to do both of those things that are safe. Why would you think you couldn't look anything up or do any logic here?
    – Servy
    Commented Oct 21, 2022 at 20:06
  • you’re right, I’ve been living in a Java world for too long 🙃
    – dst3p
    Commented Oct 22, 2022 at 21:05
5

Just use the loopState that can be provided.

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

Look at this MSDN article for an example.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.