ActionBlock<TInput>.Completion Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém um Task objeto 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 de Propriedade
A tarefa concluída.
Implementações
Exemplos
O exemplo seguinte mostra como usar a Completion propriedade de esperar que todas as mensagens se propaguem pela rede. Este exemplo de código faz parte de um exemplo mais amplo fornecido para o tópico Como: Especificar o Grau de Paralelismo num Bloco de Fluxo de Dados .
// 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
Observações
Um bloco de fluxo de dados é considerado concluído quando não está atualmente a processar uma mensagem e quando garantiu que não irá processar mais mensagens. O retorno Task transitará para um estado completo quando o bloco associado estiver concluído. Irá transitar para o RanToCompletion estado quando o bloco concluir o seu processamento com sucesso, de acordo com a semântica definida do bloco de fluxo de dados. Irá transitar para o Faulted estado quando o bloco dataflow concluir o processamento prematuramente devido a uma exceção não tratada, e fará a transição para o Canceled estado quando o bloco de dataflow concluir o processamento prematuramente após receber um pedido de cancelamento. Se a tarefa for concluída nesse Faulted estado, a sua Exception propriedade devolve uma AggregateException exceção que contém uma ou mais exceções que causaram a falha do bloco.