ActionBlock<TInput> Kelas

Definisi

Menyediakan blok aliran data yang memanggil delegasi yang disediakan Action<T> untuk setiap elemen data yang diterima.

generic <typename TInput>
public ref class ActionBlock sealed : System::Threading::Tasks::Dataflow::ITargetBlock<TInput>
public sealed class ActionBlock<TInput> : System.Threading.Tasks.Dataflow.ITargetBlock<TInput>
type ActionBlock<'Input> = class
    interface ITargetBlock<'Input>
    interface IDataflowBlock
type ActionBlock<'Input> = class
    interface IDataflowBlock
    interface ITargetBlock<'Input>
Public NotInheritable Class ActionBlock(Of TInput)
Implements ITargetBlock(Of TInput)

Jenis parameter

TInput

Jenis data tempat ini ActionBlock<TInput> beroperasi.

Warisan
ActionBlock<TInput>
Penerapan

Contoh

Contoh berikut menunjukkan penggunaan ActionBlock<TInput> kelas untuk melakukan beberapa komputasi menggunakan blok aliran data, dan mengembalikan waktu yang berlalu yang diperlukan untuk melakukan komputasi. Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk artikel Cara: Menentukan Tingkat Paralelisme dalam 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

Note

Pustaka Aliran Data TPL (namespace System.Threading.Tasks.Dataflow) tidak didistribusikan dengan .NET. Untuk menginstal namespace System.Threading.Tasks.Dataflow di Visual Studio, buka proyek Anda, pilih Kelola Paket NuGet dari menu Project, dan cari paket System.Threading.Tasks.Dataflow secara online. Atau, untuk menginstalnya menggunakan .NET Core CLI , jalankan dotnet add package System.Threading.Tasks.Dataflow.

Konstruktor

Nama Deskripsi
ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions)

Menginisialisasi instans ActionBlock<TInput> baru kelas dengan opsi tindakan dan konfigurasi yang ditentukan.

ActionBlock<TInput>(Action<TInput>)

Menginisialisasi instans ActionBlock<TInput> baru kelas dengan tindakan yang ditentukan.

ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions)

Menginisialisasi instans ActionBlock<TInput> baru kelas dengan opsi tindakan dan konfigurasi yang ditentukan.

ActionBlock<TInput>(Func<TInput,Task>)

Menginisialisasi instans ActionBlock<TInput> baru kelas dengan tindakan yang ditentukan.

Properti

Nama Deskripsi
Completion

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

InputCount

Mendapatkan jumlah item input yang menunggu untuk diproses oleh blok ini.

Metode

Nama Deskripsi
Complete()

Sinyal ke blok aliran data yang seharusnya tidak menerima atau menghasilkan pesan lagi dan tidak boleh menggunakan pesan yang ditunda lagi.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
Post(TInput)

Memposting item ke blok aliran data target.

ToString()

Mengembalikan string yang mewakili nama yang diformat dari instans ini IDataflowBlock .

Implementasi Antarmuka Eksplisit

Nama Deskripsi
IDataflowBlock.Fault(Exception)

Menyebabkan blok aliran data selesai dalam keadaan rusak.

ITargetBlock<TInput>.OfferMessage(DataflowMessageHeader, TInput, ISourceBlock<TInput>, Boolean)

Menawarkan pesan ke blok aliran data, dan memberinya kesempatan untuk mengonsumsi atau menunda pesan.

Metode Ekstensi

Nama Deskripsi
AsObserver<TInput>(ITargetBlock<TInput>)

Membuat abstraksi baru IObserver<T> melalui ITargetBlock<TInput>.

Post<TInput>(ITargetBlock<TInput>, TInput)

Memposting item ke ITargetBlock<TInput>.

SendAsync<TInput>(ITargetBlock<TInput>, TInput, CancellationToken)

Secara asinkron menawarkan pesan ke blok pesan target, memungkinkan penundaan.

SendAsync<TInput>(ITargetBlock<TInput>, TInput)

Secara asinkron menawarkan pesan ke blok pesan target, memungkinkan penundaan.

Berlaku untuk