ActionBlock<TInput> 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
ActionBlock<TInput>(Action<TInput>) |
初始化具有指定動作之 ActionBlock<TInput> 類別的新執行個體。 |
ActionBlock<TInput>(Func<TInput,Task>) |
初始化具有指定動作之 ActionBlock<TInput> 類別的新執行個體。 |
ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions) |
使用指定的動作和組態選項,初始化 ActionBlock<TInput> 類別的新執行個體。 |
ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions) |
使用指定的動作和組態選項,初始化 ActionBlock<TInput> 類別的新執行個體。 |
ActionBlock<TInput>(Action<TInput>)
初始化具有指定動作之 ActionBlock<TInput> 類別的新執行個體。
public:
ActionBlock(Action<TInput> ^ action);
public ActionBlock (Action<TInput> action);
new System.Threading.Tasks.Dataflow.ActionBlock<'Input> : Action<'Input> -> System.Threading.Tasks.Dataflow.ActionBlock<'Input>
Public Sub New (action As Action(Of TInput))
參數
- action
- Action<TInput>
要以各個接收的資料項目叫用的動作。
例外狀況
action
為 null
。
適用於
ActionBlock<TInput>(Func<TInput,Task>)
初始化具有指定動作之 ActionBlock<TInput> 類別的新執行個體。
public:
ActionBlock(Func<TInput, System::Threading::Tasks::Task ^> ^ action);
public ActionBlock (Func<TInput,System.Threading.Tasks.Task> action);
new System.Threading.Tasks.Dataflow.ActionBlock<'Input> : Func<'Input, System.Threading.Tasks.Task> -> System.Threading.Tasks.Dataflow.ActionBlock<'Input>
Public Sub New (action As Func(Of TInput, Task))
參數
例外狀況
action
為 null
。
適用於
ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions)
使用指定的動作和組態選項,初始化 ActionBlock<TInput> 類別的新執行個體。
public:
ActionBlock(Action<TInput> ^ action, System::Threading::Tasks::Dataflow::ExecutionDataflowBlockOptions ^ dataflowBlockOptions);
public ActionBlock (Action<TInput> action, System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions dataflowBlockOptions);
new System.Threading.Tasks.Dataflow.ActionBlock<'Input> : Action<'Input> * System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions -> System.Threading.Tasks.Dataflow.ActionBlock<'Input>
Public Sub New (action As Action(Of TInput), dataflowBlockOptions As ExecutionDataflowBlockOptions)
參數
- action
- Action<TInput>
要以各個接收的資料項目叫用的動作。
- dataflowBlockOptions
- ExecutionDataflowBlockOptions
用來設定這個ActionBlock<TInput> 的選項。
例外狀況
範例
下列範例示範如何使用 建 ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions) 構函式來建立新的 ActionBlock<TInput> 物件。 此程式碼範例是 如何:在資料流程區塊主題中指定平行處理原則程度 之較大範例的一部分。
// Performs several computations by using dataflow and returns the elapsed
// time required to perform the computations.
static TimeSpan TimeDataflowComputations(int maxDegreeOfParallelism,
int messageCount)
{
// Create an ActionBlock<int> that performs some work.
var workerBlock = new ActionBlock<int>(
// Simulate work by suspending the current thread.
millisecondsTimeout => Thread.Sleep(millisecondsTimeout),
// Specify a maximum degree of parallelism.
new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = maxDegreeOfParallelism
});
// Compute the time that it takes for several messages to
// flow through the dataflow block.
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < messageCount; i++)
{
workerBlock.Post(1000);
}
workerBlock.Complete();
// Wait for all messages to propagate through the network.
workerBlock.Completion.Wait();
// Stop the timer and return the elapsed number of milliseconds.
stopwatch.Stop();
return stopwatch.Elapsed;
}
' Demonstrates how to specify the maximum degree of parallelism
' when using dataflow.
Friend Class Program
' Performs several computations by using dataflow and returns the elapsed
' time required to perform the computations.
Private Shared Function TimeDataflowComputations(ByVal maxDegreeOfParallelism As Integer, ByVal messageCount As Integer) As TimeSpan
' Create an ActionBlock<int> that performs some work.
Dim workerBlock = New ActionBlock(Of Integer)(Function(millisecondsTimeout) Pause(millisecondsTimeout), New ExecutionDataflowBlockOptions() With { .MaxDegreeOfParallelism = maxDegreeOfParallelism})
' Simulate work by suspending the current thread.
' Specify a maximum degree of parallelism.
' Compute the time that it takes for several messages to
' flow through the dataflow block.
Dim stopwatch As New Stopwatch()
stopwatch.Start()
For i As Integer = 0 To messageCount - 1
workerBlock.Post(1000)
Next i
workerBlock.Complete()
' Wait for all messages to propagate through the network.
workerBlock.Completion.Wait()
' Stop the timer and return the elapsed number of milliseconds.
stopwatch.Stop()
Return stopwatch.Elapsed
End Function
Private Shared Function Pause(ByVal obj As Object)
Thread.Sleep(obj)
Return Nothing
End Function
適用於
ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions)
使用指定的動作和組態選項,初始化 ActionBlock<TInput> 類別的新執行個體。
public:
ActionBlock(Func<TInput, System::Threading::Tasks::Task ^> ^ action, System::Threading::Tasks::Dataflow::ExecutionDataflowBlockOptions ^ dataflowBlockOptions);
public ActionBlock (Func<TInput,System.Threading.Tasks.Task> action, System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions dataflowBlockOptions);
new System.Threading.Tasks.Dataflow.ActionBlock<'Input> : Func<'Input, System.Threading.Tasks.Task> * System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions -> System.Threading.Tasks.Dataflow.ActionBlock<'Input>
Public Sub New (action As Func(Of TInput, Task), dataflowBlockOptions As ExecutionDataflowBlockOptions)
參數
- dataflowBlockOptions
- ExecutionDataflowBlockOptions
用來設定這個ActionBlock<TInput> 的選項。