Parallel::ForEach<TSource> Method (IEnumerable<TSource>, Action<TSource>)
Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel.
Assembly: mscorlib (in mscorlib.dll)
public: generic<typename TSource> static ParallelLoopResult ForEach( IEnumerable<TSource>^ source, Action<TSource>^ body )
The type of the data in the source.
- Type: System.Collections.Generic::IEnumerable<>
An enumerable data source.
- Type: System::Action<>
The delegate that is invoked once per iteration.
Return ValueType: System.Threading.Tasks::ParallelLoopResult
A structure that contains information about which portion of the loop completed.
The following example uses the method to count the number of vowels and non-whitespace characters in a text file. In this case, the ParallelLoopResult value returned by the method is ignored. Note that, because operations can run in parallel, you must ensure that incrementing the counter variables is an atomic operation, and that multiple threads do not attempt to access the counter variables simultaneously. For this purpose, the example uses the lock statement (in C#) and the SyncLock statement (in Visual Basic).