System.Threading.Parallel.ParallelForEach<T> Class

public sealed class ParallelForEach<T>: ParallelLoop<T>

Base Types

Object
  ParallelLoop<T>
    ParallelForEach<T>

Assembly

System.Threading.Parallel

Library

Parallel

Summary

A parallel loop over a collection containing types of T.

Description

A ParallelForEach<T> iterates over an enumerable collection. Method System.Threading.Parallel.ParallelForEach<T>.BeginRun activates processing of the iterations, using a callback provided. The collection shall not change while the ParallelForEach<T> is active, otherwise the behavior is undefined. Inherited method System.Threading.Parallel.ParallelLoop<T>.EndRun blocks until all iterations are finished. Inherited method System.Threading.Parallel.ParallelLoop<T>.Run is shorthand for System.Threading.Parallel.ParallelForEach<T>.BeginRun and System.Threading.Parallel.ParallelLoop<T>.EndRun.

[Note: ParallelForEach<T> is generally none-scalable in terms of parallelism, because the enumerator is inherently sequential. If the collection allows random access, consider using class System.Threading.Parallel.ParalleFor instead. ]

See Also

System.Threading.Parallel Namespace

Members

ParallelForEach<T> Constructors

ParallelForEach<T>(System.IEnumerable<T>) Constructor
ParallelForEach<T>(System.IEnumerable<T>, int) Constructor

ParallelForEach<T> Methods

ParallelForEach<T>.BeginRun Method
ParallelForEach<T>.Cancel Method


ParallelForEach<T>(System.IEnumerable<T>) Constructor

public ParallelForEach(IEnumerable<T> collection);

Summary

Constructs a ParallelForEach<T> for iterating over a collection.

Parameters

collection
collection of values over which to iterate

Description

The loop does not start executing until at least method System.Threading.Parallel.ParallelForEach<T>.BeginRun is called and possibly not until method System.Threading.Parallel.ParallelLoop<T>.EndRun is called.

See Also

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

ParallelForEach<T>(System.IEnumerable<T>, int) Constructor

public ParallelForEach(IEnumerable<T> collection, int numThreads);

Summary

Constructs a ParallelForEach<T> for iterating over a collection.

Parameters

collection
collection of values over which to iterate
numThreads
maximum number of threads to use

Exceptions

Exception TypeCondition
ArgumentExceptionThe value for numThreads is negative

Description

The loop does not start executing until at least method System.Threading.Parallel.ParallelForEach<T>.BeginRun is called and possibly not until method System.Threading.Parallel.ParallelLoop<T>.EndRun is called.

If numThreads is 0, then up to System.Threading.Parallel.ParallelEnvironment.MaxThreads threads are used instead. The value includes the thread that created the System.Threading.Parallel.ParallelFor<T>, hence using numThreads=1 causes sequential execution.

See Also

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

ParallelForEach<T>.BeginRun Method

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

Summary

Begin executing iterations.

Parameters

action
The Delegate that processes each work item.

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 ParallelWhile<T>.

[Note: Implementations, particularly on single-threaded hardware, are free to employ the calling thread to execute all loop iterations.]

See Also

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

ParallelForEach<T>.Cancel Method

public override void Cancel();

Summary

Cancel any iterations that have not yet started

Description

This method is safe to call concurrently on the same instance.

Does not cancel any future iterations that might be added.

See Also

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