System.Threading.Parallel.ParallelLoop<T> Class

public abstract class ParallelLoop<T> : IDisposable

Base Types

Object
  ParallelLoop<T>

This type implements IDisposable.

Assembly

System.Threading.Parallel

Library

Parallel

Summary

A parallel loop over iteration values of type T.

Description

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.

See Also

System.Threading.Parallel Namespace

Members

ParallelLoop<T> Methods

ParallelLoop<T>.BeginRun Method
ParallelLoop<T>.Cancel Method
ParallelLoop<T>.EndRun Method
ParallelLoop<T>.Run Method


ParallelLoop<T>.BeginRun Method

public abstract void BeginRun(Action<T> action);

Summary

Begin executing iterations, applying the action delegate to each iteration value.

Parameters

action
The Delegateto apply to each iteration value.

Exceptions

Exception TypeCondition
ArgumentNullExceptionaction is null .

Description

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

See Also

System.Threading.Parallel.ParallelLoop<T> Class, System.Threading.Parallel Namespace

ParallelLoop<T>.Cancel Method

public abstract void Cancel();

Summary

Eventually cancel issuance of any further iterations

Description

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.

See Also

System.Threading.Parallel.ParallelLoop<T> Class, System.Threading.Parallel Namespace

ParallelLoop<T>.EndRun Method

public void EndRun();

Summary

Wait until all iterations are finished (or cancelled).

Description

This method is not thread safe. It should be called exactly once by the thread that called System.Threading.Parallel.ParallelLoop<T>.BeginRun.

See Also

System.Threading.Parallel.ParallelLoop<T> Class, System.Threading.Parallel Namespace

ParallelLoop<T>.Run Method

public void Run(Action<T> action);

Summary

Start processing of loop iterations and wait until done.

Parameters

action
The Delegate applied to each iteration value

Exceptions

Exception TypeCondition
ArgumentNullExceptionaction is null .

Description

This method is equivalent to calling System.Threading.Parallel.ParallelLoop<T>.BeginRun followed by calling System.Threading.Parallel.ParallelLoop<T>.EndRun.

See Also

System.Threading.Parallel.ParallelLoop<T> Class, System.Threading.Parallel Namespace