Enables iterations of parallel loops to interact with other iterations. An instance of this class is provided by the Parallel class to each loop; you can not create instances in your code.
Assemblies: System.Threading.Tasks.Parallel (in System.Threading.Tasks.Parallel.dll)
mscorlib (in mscorlib.dll)
Thetype exposes the following members.
|IsExceptional||Gets whether any iteration of the loop has thrown an exception that went unhandled by that iteration.|
|IsStopped||Gets whether any iteration of the loop has called the Stop method.|
|LowestBreakIteration||Gets the lowest iteration of the loop from which Break was called.|
|ShouldExitCurrentIteration||Gets whether the current iteration of the loop should exit based on requests made by this or other iterations.|
|Break||Communicates that the Parallel loop should cease execution of iterations beyond the current iteration at the system's earliest convenience.|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|Stop||Communicates that the Parallel loop should cease execution at the system's earliest convenience.|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
Note that you cannot instantiate an instance of this class. It is automatically generated by the compiler as an argument in a call to the Parallel.For or Parallel.ForEach method. The example provides an illustration.
Constructs such as for and foreach (in C#) and For and For Each (in Visual Basic) execute sequentially from the lowest index to the highest or from the first object in a set to the last. In contrast, the Parallel.For and Parallel.ForEach methods do not. Because individual iterations of the loop run in parallel, they can begin and end in any order. The class allows individual iterations of parallel loops to interact with one another. The class allows you to:
Exit the current iteration and prevent any additional iterations from starting by calling the Stop method. This does not affect iterations that have already begun execution.
Prevent any iterations with an index greater than the current index from executing by calling the Break method. This does not affect iterations that have already begun execution.
Determine whether an exception has occurred in any loop iteration by retrieving the value of the IsExceptional property.
Determine whether any iteration of the loop has called the Stop method by retrieving the value of the IsStopped property. You can use this property to return from iterations of the loop that started before the call to the Stop method but are still executing.
Exit from a long-running iteration whose index is greater than the index of an iteration in which Break was called by retrieving the value of the LowestBreakIteration property.
The following example executes up to 100 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 Break method is called. As the output from the example shows, no iterations whose index is greater than the LowestBreakIteration property value start after the call to the Break method.
Because iterations of the loop are still likely to be executing when the Break method is called, each iteration calls the ShouldExitCurrentIteration property to check whether another iteration has called the Break method. If the property value is true, the iteration checks the value of the LowestBreakIteration property and, if it is greater than the current iteration's index value, returns immediately.
A instance is meant to be used only within the body of the loop to which an instance was provided. It is not safe to use a provided instance after the associated loop ends, nor is it safe to explicitly hand it off to other threads and have those threads access it at any time. A different instance will be provided to each thread involved in a loop.