ParallelLoopState::Stop Method ()
Communicates that the Parallel loop should cease execution at the system's earliest convenience.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| InvalidOperationException |
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.
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.
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
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.