Export (0) Print
Expand All
Expand Minimize

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

.NET Framework 4.6 and 4.5

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

Namespace:  System.Threading.Tasks
Assemblies:   mscorlib (in mscorlib.dll)
  System.Threading.Tasks.Parallel (in System.Threading.Tasks.Parallel.dll)

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

Type Parameters


The type of the elements in source.


Type: System.Collections.Concurrent.Partitioner(Of TSource)

The partitioner that contains the original 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.


The source argument is Nothing.


The body argument is Nothing.


The SupportsDynamicPartitions property in the source partitioner returns false.


The exception that is thrown when any methods in the source partitioner return Nothing.


The GetPartitions method in the source partitioner does not return the correct number of partitions.

This overload is provided for scenarios where you want to override the default partitioning scheme. For example, small loop bodies might benefit from partitioning the range. The Parallel.ForEach method expects custom partitioners to support dynamic partitioning. For more information, see Custom Partitioners for PLINQ and TPL and How to: Implement Dynamic Partitions.

The following example shows how to implement a range partitioner for use with Parallel.ForEach:

Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks
Module RangePartitionerDemo

    Sub Main()
        Dim sw As Stopwatch = Nothing 

        Dim sum As Long = 0
        Dim SUMTOP As Long = 10000000

        ' Try sequential for
        sw = Stopwatch.StartNew()
        For i As Long = 0 To SUMTOP - 1
            sum += i
        Console.WriteLine("sequential for result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)

        ' Try parallel for with locals
        sum = 0
        sw = Stopwatch.StartNew()
        Parallel.For(0L, SUMTOP, Function() 0L, Function(item, state, prevLocal) prevLocal + item, Function(local) Interlocked.Add(sum, local))
        Console.WriteLine("parallel for w/locals result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)

        ' Try range partitioner
        sum = 0
        sw = Stopwatch.StartNew()
        Parallel.ForEach(Partitioner.Create(0L, SUMTOP),
                             Dim local As Long = 0
                             For i As Long = range.Item1 To range.Item2 - 1
                                 local += i
                             Interlocked.Add(sum, local)
                         End Sub)
        Console.WriteLine("range partitioner result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)
    End Sub 

End Module

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1
© 2015 Microsoft