ActionBlock<TInput>.Completion Proprietà

Definizione

Ottiene un Task oggetto che rappresenta l'operazione asincrona e il completamento del blocco del flusso di dati.

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

Valore della proprietà

Attività completata.

Implementazioni

Esempio

Nell'esempio seguente viene illustrato come utilizzare la Completion proprietà per attendere la propagazione di tutti i messaggi attraverso la rete. Questo esempio di codice fa parte di un esempio più ampio fornito per l'argomento Procedura: Specificare il grado di parallelismo in un blocco di flussi di dati .

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

Commenti

Un blocco di flussi di dati viene considerato completato quando non elabora attualmente un messaggio e quando ha garantito che non elaborerà altri messaggi. L'oggetto restituito Task passerà a uno stato completato al termine del blocco associato. Passerà allo RanToCompletion stato quando il blocco completa correttamente l'elaborazione in base alla semantica definita del blocco di flussi di dati. Passerà allo stato quando il blocco del flusso di dati ha completato l'elaborazione Faulted prematura a causa di un'eccezione non gestita e passerà allo Canceled stato quando il blocco del flusso di dati ha completato l'elaborazione prematuramente dopo la ricezione di una richiesta di annullamento. Se l'attività Faulted viene completata nello stato, la relativa Exception proprietà restituisce un'eccezione AggregateException che contiene una o più eccezioni che hanno causato l'esito negativo del blocco.

Si applica a