Share via


ActionBlock<TInput>.Completion 속성

정의

비동기 작업 및 데이터 흐름 블록의 완료를 나타내는 Task 개체를 가져옵니다.

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

속성 값

Task

완료된 작업입니다.

구현

예제

다음 예제에서는 모든 메시지가 네트워크를 통해 전파 될 때까지 대기 하는 속성을 사용 Completion 하는 방법을 보여 있습니다. 이 코드 예제는 방법: 데이터 흐름 블록 토픽에서 병렬 처리 수준 지정에 제공된 더 큰 예제의 일부입니다.

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

설명

데이터 흐름 블록은 현재 메시지를 처리하지 않고 더 이상 메시지를 처리하지 않을 것이라고 보장된 경우 완료된 것으로 간주됩니다. 연결된 블록이 완료되면 반환 Task 된 상태가 완료된 상태로 전환됩니다. 블록이 데이터 흐름 블록의 RanToCompletion 정의된 의미 체계에 따라 처리를 성공적으로 완료하면 상태로 전환됩니다. 처리되지 않은 예외로 Faulted 인해 데이터 흐름 블록이 조기에 처리를 완료한 경우 상태로 전환되며, 취소 요청을 받은 후 데이터 흐름 블록이 조기에 처리를 완료한 경우 상태로 전환 Canceled 됩니다. 태스크가 상태에서 완료되면 Faulted 해당 Exception 속성은 블록이 실패하게 한 하나 이상의 예외를 포함하는 예외를 반환 AggregateException 합니다.

적용 대상