ActionBlock<TInput> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供一個資料流區塊,為每個收到的資料元素調用所提供的 Action<T> 代理。
generic <typename TInput>
public ref class ActionBlock sealed : System::Threading::Tasks::Dataflow::ITargetBlock<TInput>
public sealed class ActionBlock<TInput> : System.Threading.Tasks.Dataflow.ITargetBlock<TInput>
type ActionBlock<'Input> = class
interface ITargetBlock<'Input>
interface IDataflowBlock
type ActionBlock<'Input> = class
interface IDataflowBlock
interface ITargetBlock<'Input>
Public NotInheritable Class ActionBlock(Of TInput)
Implements ITargetBlock(Of TInput)
類型參數
- TInput
這個運作的類型 ActionBlock<TInput> 。
- 繼承
-
ActionBlock<TInput>
- 實作
範例
以下範例展示了使用 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
建構函式
| 名稱 | Description |
|---|---|
| ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions) |
初始化一個新的類別實例 ActionBlock<TInput> ,並依照指定的動作與設定選項。 |
| ActionBlock<TInput>(Action<TInput>) |
以指定的動作初始化該 ActionBlock<TInput> 類別的新實例。 |
| ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions) |
初始化一個新的類別實例 ActionBlock<TInput> ,並依照指定的動作與設定選項。 |
| ActionBlock<TInput>(Func<TInput,Task>) |
以指定的動作初始化該 ActionBlock<TInput> 類別的新實例。 |
屬性
| 名稱 | Description |
|---|---|
| Completion |
取得 Task 一個代表非同步操作與資料流程區塊完成的物件。 |
| InputCount |
取得該區塊等待處理的輸入項目數量。 |
方法
| 名稱 | Description |
|---|---|
| Complete() |
向資料流區塊發出訊號,表示不應接受或產生更多訊息,也不應再消費延後訊息。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Post(TInput) |
向目標資料流區塊發佈一個項目。 |
| ToString() |
回傳一個字串,代表該 IDataflowBlock 實例格式化名稱。 |
明確介面實作
| 名稱 | Description |
|---|---|
| IDataflowBlock.Fault(Exception) |
會導致資料流區塊以錯誤狀態完成。 |
| ITargetBlock<TInput>.OfferMessage(DataflowMessageHeader, TInput, ISourceBlock<TInput>, Boolean) |
向資料流區塊提供訊息,並給予其消費或延後該訊息的機會。 |
擴充方法
| 名稱 | Description |
|---|---|
| AsObserver<TInput>(ITargetBlock<TInput>) |
在 上建立一個新的IObserver<T>抽象。ITargetBlock<TInput> |
| Post<TInput>(ITargetBlock<TInput>, TInput) |
發佈一項物品到 ITargetBlock<TInput>. |
| SendAsync<TInput>(ITargetBlock<TInput>, TInput, CancellationToken) |
非同步地向目標訊息區塊提供訊息,允許延後。 |
| SendAsync<TInput>(ITargetBlock<TInput>, TInput) |
非同步地向目標訊息區塊提供訊息,允許延後。 |