Share via


ActionBlock<TInput> 類別

定義

提供資料流程區塊,為每個收到的資料項目叫用提供的 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

備註

注意

TPL 資料流程程式庫 (System.Threading.Tasks.Dataflow 命名空間) 並未隨 .NET 散發。 若要在 Visual Studio 中安裝 System.Threading.Tasks.Dataflow 命名空間,請開啟您的專案,從 [專案]**** 功能表中選擇 [管理 NuGet 套件]****,並於線上搜尋 System.Threading.Tasks.Dataflow 套件。 除此之外也可使用 .Net Core CLI (執行 dotnet add package System.Threading.Tasks.Dataflow) 加以安裝。

建構函式

ActionBlock<TInput>(Action<TInput>)

初始化具有指定動作之 ActionBlock<TInput> 類別的新執行個體。

ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions)

使用指定的動作和組態選項,初始化 ActionBlock<TInput> 類別的新執行個體。

ActionBlock<TInput>(Func<TInput,Task>)

初始化具有指定動作之 ActionBlock<TInput> 類別的新執行個體。

ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions)

使用指定的動作和組態選項,初始化 ActionBlock<TInput> 類別的新執行個體。

屬性

Completion

取得 Task 物件,表示非同步作業和資料流程區塊的完成。

InputCount

取得等候由這個區塊處理的輸入項目的數目。

方法

Complete()

向資料流程區塊發出訊號,表示不應接受或產生任何其他訊息,也不應使用任何其他延後的訊息。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Post(TInput)

將項目張貼至目標資料流程區塊。

ToString()

傳回表示這個 IDataflowBlock 執行個體之格式化名稱的字串。

明確介面實作

IDataflowBlock.Fault(Exception)

造成資料流程區塊在錯誤 (Faulted) 狀態下完成。

ITargetBlock<TInput>.OfferMessage(DataflowMessageHeader, TInput, ISourceBlock<TInput>, Boolean)

提供訊息給資料流程區塊,讓它有機會使用或延後訊息。

擴充方法

AsObserver<TInput>(ITargetBlock<TInput>)

建立在 ITargetBlock<TInput> 之上的新 IObserver<T> Abstraction。

Post<TInput>(ITargetBlock<TInput>, TInput)

將項目張貼至 ITargetBlock<TInput>

SendAsync<TInput>(ITargetBlock<TInput>, TInput)

非同步提供訊息給目標訊息區,並允許延遲。

SendAsync<TInput>(ITargetBlock<TInput>, TInput, CancellationToken)

非同步提供訊息給目標訊息區,並允許延遲。

適用於