Export (0) Print
Expand All

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

.NET Framework 4.5

Initializes a new instance of the ActionBlock<TInput> class with the specified action and configuration options.

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

public ActionBlock(
	Action<TInput> action,
	ExecutionDataflowBlockOptions dataflowBlockOptions
)

Parameters

action
Type: System.Action<TInput>

The action to invoke with each data element received.

dataflowBlockOptions
Type: System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions

The options with which to configure this ActionBlock<TInput>.

ExceptionCondition
ArgumentNullException

action is null.

-or-

dataflowBlockOptions is null.

The following example shows how to use the ActionBlock<TInput> constructor to create a new ActionBlock<TInput> object. 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
Show:
© 2015 Microsoft