Export (0) Print
Expand All

Parallel.Invoke Method (Action())

Executes each of the provided actions, possibly in parallel.

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

'Declaration
Public Shared Sub Invoke ( _
	ParamArray actions As Action() _
)

Parameters

actions
Type: System.Action()
An array of Action to execute.

ExceptionCondition
ArgumentNullException

The actions argument is Nothing.

AggregateException

The exception that is thrown when any action in the actions array throws an exception.

ArgumentException

The actions array contains a Nothing element.

This method can be used to execute a set of operations, potentially in parallel.

No guarantees are made about the order in which the operations execute or whether they execute in parallel. This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination.

For more information, see How to: Use Parallel.Invoke to Execute Parallel Operations.

This example demonstrates how to use the Invokemethod with other methods, anonymous delegates, and lambda expressions.


Imports System.Threading
Imports System.Threading.Tasks

Module InvokeDemo

    ' Demonstrated features:
    '   Parallel.Invoke()
    ' Expected results:
    '   The threads on which each task gets executed may be different.
    '   The thread assignments may be different in different executions.
    '   The tasks may get executed in any order.
    ' Documentation:
    '   http://msdn.microsoft.com/en-us/library/dd783942(VS.100).aspx
    Private Sub Main()
        Try
            ' Param #0 - static method
            Parallel.Invoke(AddressOf BasicAction,
                            Sub()
                                ' Param #1 - lambda expression
                                Console.WriteLine("Method=beta, Thread={0}", Thread.CurrentThread.ManagedThreadId)
                            End Sub,
                            Sub()
                                ' Param #2 - in-line delegate
                                Console.WriteLine("Method=gamma, Thread={0}", Thread.CurrentThread.ManagedThreadId)
                            End Sub)
        Catch e As AggregateException
            ' No exception is expected in this example, but if one is still thrown from a task,
            ' it will be wrapped in AggregateException and propagated to the main thread.
            Console.WriteLine("An action has thrown an exception. THIS WAS UNEXPECTED." & vbLf & "{0}", e.InnerException.ToString())
        End Try
    End Sub

    Private Sub BasicAction()
        Console.WriteLine("Method=alpha, Thread={0}", Thread.CurrentThread.ManagedThreadId)
    End Sub



End Module


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft