ActionBlock<TInput> Class

.NET Framework 4.5

Provides a dataflow block that invokes a provided Action<T> delegate for every data element received.

System.Object
  System.Threading.Tasks.Dataflow.ActionBlock<TInput>

Namespace:  System.Threading.Tasks.Dataflow
Assembly:  System.Threading.Tasks.Dataflow (in System.Threading.Tasks.Dataflow.dll)

public sealed class ActionBlock<TInput> : ITargetBlock<TInput>, 
	IDataflowBlock

Type Parameters

TInput

The type of data that this ActionBlock<TInput> operates on.

The ActionBlock<TInput> type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibraryActionBlock<TInput>(Action<TInput>)Initializes a new instance of the ActionBlock<TInput> class with the specified action.
Public methodSupported by Portable Class LibraryActionBlock<TInput>(Func<TInput, Task>)Initializes a new instance of the ActionBlock<TInput> class with the specified action.
Public methodSupported by Portable Class LibraryActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions)Initializes a new instance of the ActionBlock<TInput> class with the specified action and configuration options.
Public methodSupported by Portable Class LibraryActionBlock<TInput>(Func<TInput, Task>, ExecutionDataflowBlockOptions)Initializes a new instance of the ActionBlock<TInput> class with the specified action and configuration options.
Top

  NameDescription
Public propertySupported by Portable Class LibraryCompletionGets a Task object that represents the asynchronous operation and completion of the dataflow block.
Public propertySupported by Portable Class LibraryInputCountGets the number of input items waiting to be processed by this block.
Top

  NameDescription
Public methodSupported by Portable Class LibraryCompleteSignals to the dataflow block that it shouldn't accept or produce any more messages and shouldn't consume any more postponed messages.
Public methodSupported by Portable Class LibraryEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Portable Class LibraryPostPosts an item to the target dataflow block.
Public methodSupported by Portable Class LibraryToStringReturns a string that represents the formatted name of this IDataflowBlock instance. (Overrides Object.ToString().)
Top

  NameDescription
Public Extension MethodSupported by Portable Class LibraryAsObserver<TInput>Creates a new IObserver<T> abstraction over the ITargetBlock<TInput>. (Defined by DataflowBlock.)
Public Extension MethodSupported by Portable Class LibraryPost<TInput>Posts an item to the ITargetBlock<TInput>. (Defined by DataflowBlock.)
Public Extension MethodSupported by Portable Class LibrarySendAsync<TInput>(TInput)Overloaded. Asynchronously offers a message to the target message block, allowing for postponement. (Defined by DataflowBlock.)
Public Extension MethodSupported by Portable Class LibrarySendAsync<TInput>(TInput, CancellationToken)Overloaded. Asynchronously offers a message to the target message block, allowing for postponement. (Defined by DataflowBlock.)
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by Portable Class LibraryIDataflowBlock.FaultCauses the dataflow block to complete in a faulted state.
Explicit interface implemetationPrivate methodSupported by Portable Class LibraryITargetBlock<TInput>.OfferMessageOffers a message to the dataflow block, and gives it the opportunity to consume or postpone the message.
Top

NoteNote

The TPL Dataflow Library (System.Threading.Tasks.Dataflow namespace) is not distributed with the .NET Framework 4.5. To install the System.Threading.Tasks.Dataflow namespace, open your project in Visual Studio 2012, choose Manage NuGet Packages from the Project menu, and search online for the Microsoft.Tpl.Dataflow package.

The following example shows the use of the ActionBlock<TInput> class to perform several computations using dataflow blocks, and returns the elapsed time required to perform the computations. This code example is part of a larger example provided for the How to: Specify the Degree of Parallelism in a Dataflow Block topic.

// 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;
}

.NET Framework

Supported in: 4.5

Portable Class Library

Supported in: Portable Class Library

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft