Parallel.Invoke Parallel.Invoke Parallel.Invoke Parallel.Invoke Method

Definición

Ejecuta cada una de las acciones proporcionadas, posiblemente en paralelo.Executes each of the provided actions, possibly in parallel.

Sobrecargas

Invoke(Action[]) Invoke(Action[]) Invoke(Action[]) Invoke(Action[])

Ejecuta cada una de las acciones proporcionadas, posiblemente en paralelo.Executes each of the provided actions, possibly in parallel.

Invoke(ParallelOptions, Action[]) Invoke(ParallelOptions, Action[]) Invoke(ParallelOptions, Action[])

Ejecuta todas las acciones proporcionadas, posiblemente en paralelo, a menos que el usuario cancele la operación.Executes each of the provided actions, possibly in parallel, unless the operation is cancelled by the user.

Invoke(Action[]) Invoke(Action[]) Invoke(Action[]) Invoke(Action[])

Ejecuta cada una de las acciones proporcionadas, posiblemente en paralelo.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())

Parámetros

actions
Action[]

Matriz de Action que se va a ejecutar.An array of Action to execute.

Excepciones

El argumento actions es null.The actions argument is null.

La excepción que se produce cuando cualquier acción de la matriz actions produce una excepción.The exception that is thrown when any action in the actions array throws an exception.

La matriz actions contiene un elemento null.The actions array contains a null element.

Ejemplos

Este ejemplo muestra cómo usar el Invoke método con otros métodos, delegados anónimos y expresiones lambda.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

Comentarios

Este método puede utilizarse para ejecutar un conjunto de operaciones, potencialmente en paralelo.This method can be used to execute a set of operations, potentially in parallel.

Hay ninguna garantía sobre el orden en que se ejecutan las operaciones o si se ejecutan en paralelo.No guarantees are made about the order in which the operations execute or whether they execute in parallel. Este método no devuelve hasta que se ha completado cada una de las operaciones proporcionadas, independientemente de si se produce la finalización debido a la terminación normal o excepcional.This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination.

Para más información, consulte How to: Use Parallel.Invoke to Execute Parallel Operations (Usar Parallel.Invoke para ejecutar operaciones paralelas).For more information, see How to: Use Parallel.Invoke to Execute Parallel Operations.

Invoke(ParallelOptions, Action[]) Invoke(ParallelOptions, Action[]) Invoke(ParallelOptions, Action[])

Ejecuta todas las acciones proporcionadas, posiblemente en paralelo, a menos que el usuario cancele la operación.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

Parámetros

parallelOptions
ParallelOptions ParallelOptions ParallelOptions ParallelOptions

Objeto que configura el comportamiento de esta operación.An object that configures the behavior of this operation.

actions
Action[]

Matriz de acciones que se va a ejecutar.An array of actions to execute.

Excepciones

El argumento actions es null.The actions argument is null.

O bien-or- El argumento parallelOptions es null.The parallelOptions argument is null.

La excepción que se produce cuando cualquier acción de la matriz actions produce una excepción.The exception that is thrown when any action in the actions array throws an exception.

La matriz actions contiene un elemento null.The actions array contains a null element.

El objeto CancellationTokenSource asociado a CancellationToken en el parallelOptions se ha desechado.The CancellationTokenSource associated with the CancellationToken in the parallelOptions has been disposed.

Comentarios

Este método puede utilizarse para ejecutar un conjunto de operaciones, potencialmente en paralelo.This method can be used to execute a set of operations, potentially in parallel. El token de cancelación pasado con la ParallelOptions estructura permite que el llamador cancelar la operación completa.The cancellation token passed in with the ParallelOptions structure enables the caller to cancel the entire operation. Para más información, consulte el tema sobre la cancelación en subprocesos administrados.For more information, see Cancellation in Managed Threads.

Hay ninguna garantía sobre el orden en que se ejecutan las operaciones o si se ejecutan en paralelo.No guarantees are made about the order in which the operations execute or whether they execute in parallel. Este método no devuelve hasta que se ha completado cada una de las operaciones proporcionadas, independientemente de si se produce la finalización debido a la terminación normal o excepcional.This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination.

Para más información, consulte How to: Use Parallel.Invoke to Execute Parallel Operations (Usar Parallel.Invoke para ejecutar operaciones paralelas).For more information, see How to: Use Parallel.Invoke to Execute Parallel Operations.

Se aplica a