Parallel.Invoke 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
執行每個提供的動作,可能是並行執行。
多載
| 名稱 | Description |
|---|---|
| Invoke(Action[]) |
執行每個提供的動作,可能是並行執行。 |
| Invoke(ParallelOptions, Action[]) |
執行每個提供的動作,可能並行執行,除非使用者取消該操作。 |
Invoke(Action[])
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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 點為 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[])
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- Parallel.cs
- 來源:
- 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 個已經設定好了。
當陣列中 actions 任一動作拋出例外時,會拋出的例外。
陣 actions 列包含一個 null 元素。
CancellationTokenSource與相關CancellationTokenparallelOptions事物已處置。
備註
此方法可用於執行一組操作,且可能並行進行。 隨 ParallelOptions 結構一同傳送的取消標記允許呼叫者取消整個操作。 如需詳細資訊,請參閱 管理線程中的取消操作。
對於操作執行的順序或是否並行執行,並不保證。 無論完成是因正常或特殊終止,此方法在每個提供操作完成前不會回傳。
如需詳細資訊,請參閱 如何:使用 Parallel.Invoke 執行平行作業。