ActionBlock<TInput>.Completion Eigenschaft
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ruft ein Task Objekt ab, das den asynchronen Vorgang und den Abschluss des Datenflussblocks darstellt.
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
Eigenschaftswert
Die abgeschlossene Aufgabe.
Implementiert
Beispiele
Das folgende Beispiel zeigt, wie die Completion Eigenschaft verwendet wird, um zu warten, bis alle Nachrichten über das Netzwerk verteilt werden. Dieses Codebeispiel ist Teil eines größeren Beispiels, das für das Thema " How to: Specify the Degree of Parallelism in a Dataflow Block " bereitgestellt wird.
// 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
Hinweise
Ein Datenflussblock wird als abgeschlossen betrachtet, wenn er derzeit keine Nachricht verarbeitet, und wenn sichergestellt ist, dass keine weiteren Nachrichten verarbeitet werden. Der zurückgegebene Wechsel wechselt zu einem abgeschlossenen Zustand, wenn der zugeordnete Task Block abgeschlossen ist. Er wechselt in den RanToCompletion Zustand, wenn der Block seine Verarbeitung erfolgreich gemäß der definierten Semantik des Datenflussblocks abgeschlossen hat. Er wechselt in den Faulted Zustand, wenn der Datenflussblock aufgrund einer unbehandelten Ausnahme vorzeitig die Verarbeitung abgeschlossen hat, und er wechselt in den Canceled Zustand, wenn der Datenflussblock die Verarbeitung vorzeitig abgeschlossen hat, nachdem eine Abbruchanforderung empfangen wurde. Wenn die Aufgabe im Faulted Zustand abgeschlossen ist, gibt die Exception Eigenschaft eine Ausnahme zurück, die mindestens AggregateException eine Ausnahme enthält, die dazu führte, dass der Block fehlschlug.