Export (0) Print
Expand All
Expand Minimize

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

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

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

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

Type Parameters

TSource

The type of the elements in source.

Parameters

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

The partitioner that contains the original data source.

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

ExceptionCondition
ArgumentNullException

The source argument is Nothing.

-or-

The body argument is Nothing.

InvalidOperationException

The SupportsDynamicPartitions property in the source partitioner returns false.

-or-

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

-or-

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
        Next
        sw.Stop()
        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))
        sw.Stop()
        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),
                         Sub(range)
                             Dim local As Long = 0
                             For i As Long = range.Item1 To range.Item2 - 1
                                 local += i
                             Next
                             Interlocked.Add(sum, local)
                         End Sub)
        sw.Stop()
        Console.WriteLine("range partitioner result = {0}, time = {1} ms", sum, sw.ElapsedMilliseconds)
    End Sub 

End Module

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft