Bagikan melalui


ActionBlock<TInput>.Completion Properti

Definisi

Task Mendapatkan objek yang mewakili operasi asinkron dan penyelesaian blok aliran data.

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

Nilai Properti

Tugas yang telah selesai.

Penerapan

Contoh

Contoh berikut menunjukkan cara menggunakan Completion properti untuk menunggu semua pesan disebarluaskan melalui jaringan. Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk Cara: Menentukan Tingkat Paralelisme dalam topik Blok Aliran Data .

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

Keterangan

Blok aliran data dianggap selesai ketika saat ini tidak memproses pesan dan ketika telah menjamin bahwa itu tidak akan memproses pesan lagi. Yang dikembalikan Task akan beralih ke status selesai ketika blok terkait telah selesai. Ini akan beralih ke RanToCompletion status ketika blok berhasil menyelesaikan pemrosesannya sesuai dengan semantik yang ditentukan blok aliran data. Ini akan beralih ke Faulted status ketika blok aliran data telah selesai diproses sebelum waktunya karena pengecualian yang tidak tertangani, dan itu akan beralih ke Canceled status ketika blok aliran data telah selesai diproses sebelum waktunya setelah menerima permintaan pembatalan. Jika tugas selesai dalam Faulted status, propertinya Exception mengembalikan AggregateException pengecualian yang berisi satu atau beberapa pengecualian yang menyebabkan blok gagal.

Berlaku untuk