Parallel.ForEach(Of TSource) Method (IEnumerable(Of TSource), Action(Of TSource))

.NET Framework (current version)

Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel.

Namespace:   System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Function ForEach(Of TSource) (
	source As IEnumerable(Of TSource),
	body As Action(Of TSource)
) As ParallelLoopResult


Type: System.Collections.Generic.IEnumerable(Of TSource)

An enumerable data source.

Type: System.Action(Of TSource)

The delegate that is invoked once per iteration.

Return Value

Type: System.Threading.Tasks.ParallelLoopResult

A structure that contains information about which portion of the loop completed.

Type Parameters


The type of the data in the source.

Exception Condition

The source argument is null.


The body argument is null.


The exception that contains all the individual exceptions thrown on all threads.

The body delegate is invoked once for each element in the source enumerable. It is provided with the current element as a parameter.

The following example uses the ForEach(Of TSource)(IEnumerable(Of TSource), Action(Of TSource)) 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).

Imports System.IO
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim task As Task(Of String) = ReadCharacters(".\CallOfTheWild.txt")
      Dim text As String = task.Result

      Dim nVowels As Integer = 0
      Dim nNonWhiteSpace As Integer = 0
      Dim obj As New Object()

      Dim result As ParallelLoopResult = Parallel.ForEach(text, 
                                                             Dim uCh As Char = Char.ToUpper(ch)
                                                             If "AEIOUY".IndexOf(uCh) >= 0 Then
                                                                SyncLock obj
                                                                   nVowels += 1
                                                                End SyncLock
                                                             End If
                                                             If Not Char.IsWhiteSpace(uCh) Then
                                                                SyncLock obj
                                                                   nNonWhiteSpace += 1
                                                                End SyncLock   
                                                             End If
                                                          End Sub)
      Console.WriteLine("Total characters:      {0,10:N0}", text.Length)
      Console.WriteLine("Total vowels:          {0,10:N0}", nVowels)
      Console.WriteLine("Total non-whitespace:  {0,10:N0}", nNonWhiteSpace)
   End Sub

   Private Async Function ReadCharacters(fn As String) As Task(Of String)
      Dim text As String
      Using sr As New StreamReader(fn)
         text = Await sr.ReadToEndAsync()
      End Using
      Return text
   End Function
End Module
' The output from the example resembles the following:
'       Total characters:         198,548
'       Total vowels:              58,421
'       Total non-whitespace:     159,461

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top