O'Reilly logo

C# Multithreaded and Parallel Programming by Rodney Ringler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Canceling a parallel loop

Now that we have learned how to execute a parallel loop using a Parallel class library, let's take a look at how we can stop or break a loop if needed. With a normal For or ForEach loop we can use a Continue command to break from a loop.

If we break from a parallel loop, then we complete all iterations on the threads that are currently executing and then stop. If we Stop a parallel loop, then we stop all currently running iterations of the loop as soon as possible, but we do not run them to completion. In either case, we will not schedule tasks on the threadpool for the rest of the iterations of the parallel loop that we are yet to get started with.

To perform a break or a stop of a parallel loop, we need to use the ParallelLoopState ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required