Parallel.Invoke 方法

定义

执行每个提供的操作,可能并行执行。

重载

名称 说明
Invoke(Action[])

执行每个提供的操作,可能并行执行。

Invoke(ParallelOptions, Action[])

除非用户取消了该操作,否则执行每个提供的操作(可能并行执行)。

Invoke(Action[])

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

执行每个提供的操作,可能并行执行。

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())

参数

actions
Action[]

Action 执行的数组。

例外

参数 actionsnull.

当数组中的任何 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[])

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

除非用户取消了该操作,否则执行每个提供的操作(可能并行执行)。

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())

参数

parallelOptions
ParallelOptions

一个对象,用于配置此操作的行为。

actions
Action[]

要执行的操作数组。

例外

设置 CancellationToken 中的 parallelOptions 值。

参数 actionsnull.

-或-

参数 parallelOptionsnull.

当数组中的任何 actions 操作引发异常时引发的异常。

数组 actions 包含一个 null 元素。

CancellationTokenSource 释放与 CancellationTokenparallelOptions 关联的对象。

注解

此方法可用于执行一组可能并行的操作。 使用 ParallelOptions 结构传入的取消令牌使调用方能够取消整个操作。 有关详细信息,请参阅托管线程中的取消

不保证操作的执行顺序或操作是否并行执行。 无论由于正常终止还是异常终止而完成,此方法都不会返回,直到每个提供的操作都已完成。

有关详细信息,请参阅 如何:使用 Parallel.Invoke 执行并行操作

适用于