ActionBlock<TInput>.Completion Propiedad

Definición

Obtiene un objeto Task que representa la operación asincrónica y la finalización del bloque de flujo de datos.

public:
 property System::Threading::Tasks::Task ^ Completion { System::Threading::Tasks::Task ^ get(); };
public System.Threading.Tasks.Task Completion { get; }
member this.Completion : System.Threading.Tasks.Task
Public ReadOnly Property Completion As Task

Valor de propiedad

Task

Tarea completada.

Implementaciones

Ejemplos

En el ejemplo siguiente se muestra cómo usar la Completion propiedad para esperar a que todos los mensajes se propague a través de la red. Este ejemplo de código forma parte de un ejemplo más grande que se proporciona para el tema How to: Specify the Degree of Parallelism in a Dataflow Block (Cómo: Especificar el grado de paralelismo en un bloque de flujo de datos ).

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

Comentarios

Un bloque de flujo de datos se considera completado cuando no está procesando actualmente un mensaje y cuando ha garantizado que no procesará más mensajes. El devuelto Task pasará a un estado completado cuando se haya completado el bloque asociado. Pasará al RanToCompletion estado cuando el bloque complete su procesamiento correctamente según la semántica definida del bloque de flujo de datos. Pasará al Faulted estado cuando el bloque de flujo de datos haya completado el procesamiento prematuramente debido a una excepción no controlada y pasará al Canceled estado cuando el bloque de flujo de datos haya completado el procesamiento prematuramente después de recibir una solicitud de cancelación. Si la tarea se completa en el Faulted estado , su Exception propiedad devuelve una AggregateException excepción que contiene una o varias excepciones que provocaron un error en el bloque.

Se aplica a