ActionBlock<TInput>.Completion 属性
定义
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
属性值
已完成任务。The completed task.
实现
示例
下面的示例演示如何使用 Completion 属性等待所有消息通过网络传播。The following example shows how to use the Completion property to wait for all messages to propagate through the network. 此代码示例摘自为 如何:在数据流块中指定并行度主题中 提供的更大示例的一部分。This code example is part of a larger example provided for the How to: Specify the Degree of Parallelism in a Dataflow Block topic.
// 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
注解
数据流块在当前未处理消息时,将被视为已完成,并保证其不会再处理其他消息。A dataflow block is considered completed when it is not currently processing a message and when it has guaranteed that it will not process any more messages. Task完成关联的块后,返回的将转换为已完成状态。The returned Task will transition to a completed state when the associated block has completed. RanToCompletion当块根据数据流块的定义语义成功完成其处理时,它将转换为状态。It will transition to the RanToCompletion state when the block completes its processing successfully according to the dataflow block's defined semantics. Faulted当数据流块由于未经处理的异常而提前完成处理时,它将转换为状态,并且 Canceled 在收到取消请求后,当数据流块提前处理完毕时,它将转换为状态。It will transition to the Faulted state when the dataflow block has completed processing prematurely due to an unhandled exception, and it will transition to the Canceled state when the dataflow block has completed processing prematurely after receiving a cancellation request. 如果任务在状态中完成 Faulted ,则其 Exception 属性将返回 AggregateException 一个异常,该异常包含导致块失败的一个或多个异常。If the task completes in the Faulted state, its Exception property returns an AggregateException exception that contains one or more exceptions that caused the block to fail.