Exceptions

The Break() method was called previously. Break() and Stop() may not be used in combination by iterations of the same loop.

Examples

The following example executes up to 10,000 iterations of a loop in parallel. Each iteration pauses for a random interval from 1 to 1,000 milliseconds. A randomly generated value determines on which iteration of the loop the Stop method is called. As the output from the example shows, no iterations execute after the call to the Stop method.

Because iterations of the loop are still likely to be executing when the Stop method is called, each iteration calls the IsStopped method to check whether another iteration has called the Stop method. If it returns true, the iteration returns immediately.

Remarks

Calling the Stop method indicates that any iterations of the loop that have not yet started need not be run. It effectively cancels any additional iterations of the loop. However, it does not stop any iterations that have already begun execution.

Calling the Stop method causes the IsStopped property to return true for any iteration of the loop that is still executing. This is particularly useful for long-running iterations, which can check the IsStopped property and exit early if its value is true.

Stop is typically employed in search-based algorithms, where once a result is found, no other iterations need be executed.