ActionBlock<TInput>.Completion Property

.NET Framework (current version)

Gets a Task object that represents the asynchronous operation and completion of the dataflow block.

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

public Task Completion { get; }

Property Value

Type: System.Threading.Tasks.Task
The completed task.



A dataflow block is considered completed when it is not currently processing a message and when it has guaranteed that it will not process any more messages. The returned Task will transition to a completed state when the associated block has completed. It will transition to the TaskStatus.RanToCompletion state when the block completes its processing successfully according to the dataflow block’s defined semantics. It will transition to the TaskStatus.Faulted state when the dataflow block has completed processing prematurely due to an unhandled exception, and it will transition to the TaskStatus.Canceled state when the dataflow block has completed processing prematurely after receiving a cancellation request. If the task completes in the TaskStatus.Faulted state, its Exception property returns an AggregateException exception that contains one or more exceptions that caused the block to fail.

The following example shows how to use the Completion property to wait for all messages to propagate through the network. 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();

   for (int i = 0; i < messageCount; i++)

   // Wait for all messages to propagate through the network.

   // Stop the timer and return the elapsed number of milliseconds.
   return stopwatch.Elapsed;

.NET Framework

Supported in: 4.5

Portable Class Library

Supported in: Portable Class Library