Partilhar via


ActionBlock<TInput>.Completion Propriedade

Definição

Obtém um objeto Task que representa a operação assíncrona e a conclusão do bloco de fluxo de dados.

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 da propriedade

Task

A tarefa concluída.

Implementações

Exemplos

O exemplo a seguir mostra como usar a Completion propriedade para aguardar a propagação de todas as mensagens pela rede. Este exemplo de código faz parte de um exemplo maior fornecido para o tópico How to: Specify the Degree of Parallelism in a Dataflow Block .

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

Comentários

Um bloco de fluxo de dados é considerado concluído quando não está processando uma mensagem no momento e quando ele garantiu que não processará mais nenhuma mensagem. O retornado Task fará a transição para um estado concluído quando o bloco associado for concluído. Ele fará a transição para o RanToCompletion estado quando o bloco concluir o processamento com êxito de acordo com a semântica definida do bloco de fluxo de dados. Ele fará a transição para o Faulted estado quando o bloco de fluxo de dados tiver concluído o processamento prematuramente devido a uma exceção sem tratamento e fará a transição para o Canceled estado quando o bloco de fluxo de dados tiver concluído o processamento prematuramente após receber uma solicitação de cancelamento. Se a tarefa for concluída no Faulted estado, sua Exception propriedade retornará uma exceção AggregateException que contém uma ou mais exceções que fizeram com que o bloco falhasse.

Aplica-se a