Parallel.Invoke 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
尽可能并行执行提供的每个操作。
重载
| Invoke(Action[]) | 尽可能并行执行提供的每个操作。 | 
| Invoke(ParallelOptions, Action[]) | 执行所提供的每个操作,而且尽可能并行运行,除非用户取消了操作。 | 
Invoke(Action[])
尽可能并行执行提供的每个操作。
public:
 static void Invoke(... cli::array <Action ^> ^ actions);public static void Invoke (params Action[] actions);static member Invoke : Action[] -> unitPublic Shared Sub Invoke (ParamArray actions As Action())参数
例外
actions 参数为 null。
当 actions 数组中的任何操作引发异常时引发的异常。
actions数组包含 null 个元素。
示例
此示例演示如何将该方法与其他方法、匿名委托和 lambda 表达式配合使用 Invoke 。
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
注解
此方法可用于执行一组可能并行的操作。
无法保证操作的执行顺序或操作是否并行执行。 此方法不会返回,直到每个提供的操作都已完成,无论是否由于正常终止或异常终止而完成。
有关详细信息,请参阅如何:使用 Parallel.Invoke 来执行并行操作。
适用于
Invoke(ParallelOptions, Action[])
执行所提供的每个操作,而且尽可能并行运行,除非用户取消了操作。
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[] -> unitPublic Shared Sub Invoke (parallelOptions As ParallelOptions, ParamArray actions As Action())参数
- parallelOptions
- ParallelOptions
一个对象,用于配置此操作的行为。
- actions
- Action[]
要执行的操作数组。
例外
CancellationToken 处于 parallelOptions 设置。
当 actions 数组中的任何操作引发异常时引发的异常。
actions数组包含 null 个元素。
在 parallelOptions 中与 CancellationTokenSource 关联的 CancellationToken 已被释放。
注解
此方法可用于执行一组可能并行的操作。 使用结构传入 ParallelOptions 的取消令牌使调用方能够取消整个操作。 有关详细信息,请参阅托管线程中的取消。
无法保证操作的执行顺序或操作是否并行执行。 此方法不会返回,直到每个提供的操作都已完成,无论是否由于正常终止或异常终止而完成。
有关详细信息,请参阅如何:使用 Parallel.Invoke 来执行并行操作。