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
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