ActionBlock<TInput>
Class
Definition
Provides a dataflow block that invokes a provided Action<T> delegate for every data element received.
public sealed class ActionBlock<TInput> : System.Threading.Tasks.Dataflow.ITargetBlock<TInput>
- TInput
The type of data that this ActionBlock<TInput> operates on.
- Inheritance
-
ActionBlock<TInput>
- Implements
Inherited Members
System.Object
Examples
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;
}
' 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
Remarks
Note
The TPL Dataflow Library (System.Threading.Tasks.Dataflow namespace) is not distributed with the net_v45. 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.
Constructors
| ActionBlock<TInput>(Action<TInput>) |
Initializes a new instance of the ActionBlock<TInput> class with the specified action. |
| ActionBlock<TInput>(Func<TInput,Task>) |
Initializes a new instance of the ActionBlock<TInput> class with the specified action. |
| ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions) |
Initializes a new instance of the ActionBlock<TInput> class with the specified action and configuration options. |
| ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions) |
Initializes a new instance of the ActionBlock<TInput> class with the specified action and configuration options. |
Properties
| Completion |
Gets a Task object that represents the asynchronous operation and completion of the dataflow block. |
| InputCount |
Gets the number of input items waiting to be processed by this block. |
Methods
| Complete() |
Signals to the dataflow block that it shouldn't accept or produce any more messages and shouldn't consume any more postponed messages. |
| Post(TInput) |
Posts an item to the target dataflow block. |
| ToString() |
Returns a string that represents the formatted name of this IDataflowBlock instance. |
Explicit Interface Implementations
| IDataflowBlock.Fault(Exception) |
Causes the dataflow block to complete in a faulted state. |
| ITargetBlock<TInput>.OfferMessage(DataflowMessageHeader, TInput, ISourceBlock<TInput>, Boolean) |
Offers a message to the dataflow block, and gives it the opportunity to consume or postpone the message. |
Extension Methods
| AsObserver<TInput>(ITargetBlock<TInput>) | |
| Post<TInput>(ITargetBlock<TInput>, TInput) | |
| SendAsync<TInput>(ITargetBlock<TInput>, TInput) | |
| SendAsync<TInput>(ITargetBlock<TInput>, TInput, CancellationToken) |