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

注釈

Note

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>, ExecutionDataflowBlockOptions)

指定したアクションと構成オプションを使用して、 ActionBlock<TInput> クラスの新しいインスタンスを初期化します。

ActionBlock<TInput>(Action<TInput>)

指定したアクションを使用して、 ActionBlock<TInput> クラスの新しいインスタンスを初期化します。

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

指定したアクションと構成オプションを使用して、 ActionBlock<TInput> クラスの新しいインスタンスを初期化します。

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

指定したアクションを使用して、 ActionBlock<TInput> クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
Completion

非同期操作とデータフロー ブロックの完了を表す Task オブジェクトを取得します。

InputCount

このブロックによって処理されるのを待機している入力項目の数を取得します。

メソッド

名前 説明
Complete()

これ以上メッセージを受け入れたり生成したりしてはいけないことをデータフロー ブロックに通知し、これ以上延期されたメッセージを使用しないようにします。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
Post(TInput)

ターゲット データフロー ブロックに項目を投稿します。

ToString()

この IDataflowBlock インスタンスの書式設定された名前を表す文字列を返します。

明示的なインターフェイスの実装

名前 説明
IDataflowBlock.Fault(Exception)

データフロー ブロックがエラー状態で完了します。

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

データフロー ブロックにメッセージを提供し、メッセージを使用または延期する機会を提供します。

拡張メソッド

名前 説明
AsObserver<TInput>(ITargetBlock<TInput>)

ITargetBlock<TInput>に対する新しいIObserver<T>抽象化を作成します。

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

アイテムを ITargetBlock<TInput>に投稿します。

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

ターゲット メッセージ ブロックにメッセージを非同期的に提供し、延期できるようにします。

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

ターゲット メッセージ ブロックにメッセージを非同期的に提供し、延期できるようにします。

適用対象