Parallel.Invoke 方法

定義

執行每個提供的動作 (可能會平行執行)。

多載

Invoke(Action[])

執行每個提供的動作 (可能會平行執行)。

Invoke(ParallelOptions, Action[])

除非使用者取消作業,否則執行每個提供的動作 (可能會平行執行)。

Invoke(Action[])

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 陣列。

例外狀況

actions 引數為 null

actions 陣列中的任何動作擲回例外狀況時,所擲回的例外狀況。

actions 陣列包含 null 項目。

範例

此範例示範如何使用 Invoke 方法搭配其他方法、匿名委派和 Lambda 運算式。

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

除非使用者取消作業,否則執行每個提供的動作 (可能會平行執行)。

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[]

要執行的動作陣列。

例外狀況

parallelOptions 中的 CancellationToken 已設定。

actions 引數為 null

-或-

parallelOptions 引數為 null

actions 陣列中的任何動作擲回例外狀況時,所擲回的例外狀況。

actions 陣列包含 null 項目。

已處置與 parallelOptions 中的 CancellationTokenSource 相關聯的 CancellationToken

備註

這個方法可用來執行一組作業,可能平行執行。 使用 結構傳入的 ParallelOptions 解除標記可讓呼叫端取消整個作業。 如需詳細資訊,請參閱 受控執行緒中的取消

不會保證作業執行的順序,或作業是否以平行方式執行。 這個方法不會傳回,直到每個提供的作業都完成為止,不論完成是否因為正常或例外狀況終止而發生。

如需詳細資訊,請參閱如何:使用 Parallel.Invoke 來執行平行作業

適用於