public abstract class ParallelLoop<T> : IDisposable
Object
ParallelLoop<T>This type implements IDisposable.
System.Threading.Parallel
Parallel
A parallel loop over iteration values of type T.
Abstract generic class ParallelLoop<T> abstracts common behavior of the loop classes that iterate over values of type T. Its derived classes differ in how the iteration space is defined.Iteration commences once method System.Threading.Parallel.ParallelLoop<T>.BeginRun is called. The callback is applied to each iteration value. A conforming implementation can use the thread calling System.Threading.Parallel.ParallelLoop<T>.BeginRun to execute all iterations, regardless of the value of System.Threading.Parallel.ParallelLoop<T>.MaxThreads. The thread that calls System.Threading.Parallel.ParallelLoop<T>.BeginRun shall call method System.Threading.Parallel.ParallelLoop<T>.EndRun to block until all iterations complete or are cancelled. When System.Threading.Parallel.ParallelLoop<T>.EndRun is called, the calling thread can be employed as a worker thread.
Calling method System.Threading.Parallel.ParallelLoop<T>.Run is equivalent to calling System.Threading.Parallel.ParallelLoop<T>.BeginRun followed by calling “System.Threading.Parallel.ParallelLoop<T>.EndRun.
A parallel loop can be cancelled at any time (even before it starts running) by calling method System.Threading.Parallel.ParallelLoop<T>.Cancel. Cancellation is asynchronous in the sense that method System.Threading.Parallel.ParallelLoop<T>.Cancel can return while portions of the loop are still running. Any number of threads can call System.Threading.Parallel.ParallelLoop<T>.Cancel on the same object. Cancellation affects only iterations that have not yet been issued to worker threads. Outstanding iterations are completed normally.
If one or more invocations of a callback throws an unhandled exception, the rest of the loop is cancelled. One of the exceptions is saved inside the ParallelLoop<T> until the loop has stopped running, and then the saved exception is rethrown when method System.Threading.Parallel.ParallelLoop<T>.EndRun is invoked. In the case of multiple exceptions, the implementation can choose any one of the exceptions to save and rethrow.
System.Threading.Parallel Namespace
ParallelLoop<T> Methods
ParallelLoop<T>.BeginRun Method
ParallelLoop<T>.Cancel Method
ParallelLoop<T>.EndRun Method
ParallelLoop<T>.Run Method
public abstract void BeginRun(Action<T> action);
Begin executing iterations, applying the action delegate to each iteration value.
- action
- The Delegateto apply to each iteration value.
Exception Type Condition ArgumentNullException action is null
.
This method is not thread safe. It should be called only once for a given instance of a ParallelLoop<T>.If one or more invocations of a callback throws an unhandled exception, the rest of the loop is cancelled. One of the exceptions is saved inside the ParallelLoop<T>until the loop has stopped running, and then the saved exception is rethrown when method EndRun is invoked. In the case of multiple exceptions, the implementation can choose any one of the exceptions to save and rethrow.
[Note: Implementations, particularly on single-threaded hardware, are free to employ the calling thread to execute all loop iterations.]
[Note: The return value is void, not IAsyncResult, and there is no callBack or stateObject arguments. This departure from the usual asynchronous call pattern (e.g. FileStreamBeginRead) is deliberate, because in typical scenarios the extra complexity would just add pointless burden on the implementation.]
System.Threading.Parallel.ParallelLoop<T> Class, System.Threading.Parallel Namespace
public abstract void Cancel();
Eventually cancel issuance of any further iterations
A ParallelLoop<T> can be cancelled at any time (even before it starts running) by calling method Cancel. Cancellation is asynchronous in the sense that method Cancel can return while portions of the loop are still running. Any number of threads can concurrently call Cancel on the same object. Cancellation affects only iterations that have not yet been issued to worker threads. Outstanding iterations are completed normally.
System.Threading.Parallel.ParallelLoop<T> Class, System.Threading.Parallel Namespace
public void EndRun();
Wait until all iterations are finished (or cancelled).
This method is not thread safe. It should be called exactly once by the thread that called System.Threading.Parallel.ParallelLoop<T>.BeginRun.
System.Threading.Parallel.ParallelLoop<T> Class, System.Threading.Parallel Namespace
public void Run(Action<T> action);
Start processing of loop iterations and wait until done.
- action
- The Delegate applied to each iteration value
Exception Type Condition ArgumentNullException action is null
.
This method is equivalent to calling System.Threading.Parallel.ParallelLoop<T>.BeginRun followed by calling System.Threading.Parallel.ParallelLoop<T>.EndRun.
System.Threading.Parallel.ParallelLoop<T> Class, System.Threading.Parallel Namespace