Share via


ActionBlock<TInput>.Complete 메서드

정의

데이터 흐름 블록에 대한 신호를 통해 더 이상의 메시지를 받거나 생성할 수 없으며 추가로 지연된 메시지를 사용하면 안 됩니다.

public:
 virtual void Complete();
public void Complete ();
abstract member Complete : unit -> unit
override this.Complete : unit -> unit
Public Sub Complete ()

구현

예제

다음 예제에서는 메서드를 Complete 사용하여 더 이상 메시지를 수락하거나 생성하거나 연기된 메시지를 더 이상 사용하지 않아야 하며 데이터 흐름 블록에 신호를 보내는 방법을 보여 줍니다. 이 코드 예제는 방법: 데이터 흐름 블록 토픽에서 병렬 처리 수준 지정에 제공된 더 큰 예제의 일부입니다.

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

설명

데이터 흐름 블록에서 호출된 후에 Complete 는 이전에 사용 가능한 모든 데이터를 처리한 후 해당 블록이 완료됩니다(작업이 최종 상태가 되도록 Completion ). 이 메서드는 완료 대기를 차단하지 않지만 요청을 시작합니다. 완료될 때까지 기다리려면 속성을 사용합니다 Completion .

적용 대상