Επεξεργασία

Κοινή χρήση μέσω


Parallel.Invoke Method

Definition

Executes each of the provided actions, possibly in parallel.

Overloads

Invoke(Action[])

Executes each of the provided actions, possibly in parallel.

Invoke(ParallelOptions, Action[])

Executes each of the provided actions, possibly in parallel, unless the operation is cancelled by the user.

Invoke(Action[])

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

Executes each of the provided actions, possibly in parallel.

public:
 static void Invoke(... cli::array <Action ^> ^ actions);
public static void Invoke (params Action[] actions);
static member Invoke : Action[] -> unit
Public Shared Sub Invoke (ParamArray actions As Action())

Parameters

actions
Action[]

An array of Action to execute.

Exceptions

The actions argument is null.

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

The actions array contains a null element.

Examples

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

    class ParallelInvokeDemo
    {

        // 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/library/dd783942(VS.100).aspx
        static void Main()
        {
            try
            {
                Parallel.Invoke(
                    BasicAction,	// Param #0 - static method
                    () =>			// Param #1 - lambda expression
                    {
                        Console.WriteLine("Method=beta, Thread={0}", Thread.CurrentThread.ManagedThreadId);
                    },
                    delegate()		// Param #2 - in-line delegate
                    {
                        Console.WriteLine("Method=gamma, Thread={0}", Thread.CurrentThread.ManagedThreadId);
                    }
                );
            }
            // 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.
            catch (AggregateException e)
            {
                Console.WriteLine("An action has thrown an exception. THIS WAS UNEXPECTED.\n{0}", e.InnerException.ToString());
            }
        }

        static void BasicAction()
        {
            Console.WriteLine("Method=alpha, Thread={0}", Thread.CurrentThread.ManagedThreadId);
        }
    }
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/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

Remarks

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.

Applies to

Invoke(ParallelOptions, Action[])

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

Executes each of the provided actions, possibly in parallel, unless the operation is cancelled by the user.

public:
 static void Invoke(System::Threading::Tasks::ParallelOptions ^ parallelOptions, ... cli::array <Action ^> ^ actions);
public static void Invoke (System.Threading.Tasks.ParallelOptions parallelOptions, params Action[] actions);
static member Invoke : System.Threading.Tasks.ParallelOptions * Action[] -> unit
Public Shared Sub Invoke (parallelOptions As ParallelOptions, ParamArray actions As Action())

Parameters

parallelOptions
ParallelOptions

An object that configures the behavior of this operation.

actions
Action[]

An array of actions to execute.

Exceptions

The CancellationToken in the parallelOptions is set.

The actions argument is null.

-or-

The parallelOptions argument is null.

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

The actions array contains a null element.

The CancellationTokenSource associated with the CancellationToken in the parallelOptions has been disposed.

Remarks

This method can be used to execute a set of operations, potentially in parallel. The cancellation token passed in with the ParallelOptions structure enables the caller to cancel the entire operation. For more information, see Cancellation in Managed Threads.

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.

Applies to