ActionBlock<TInput> Klasse

Definition

Stellt einen Datenflussblock bereit, der einen bereitgestellten Action<T>-Delegaten für jede empfangene Datenelement aufruft.

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)

Typparameter

TInput

Der Datentyp, auf dem dieser ActionBlock<TInput> betrieben wird.

Vererbung
ActionBlock<TInput>
Implementiert

Beispiele

Im folgenden Beispiel wird die Verwendung der ActionBlock<TInput> Klasse gezeigt, um mehrere Berechnungen mithilfe von Datenflussblöcken durchzuführen und die für die Ausführung der Berechnungen erforderliche Zeit zurückgibt. Dieses Codebeispiel ist Teil eines größeren Beispiels für das Gewusst wie: Angeben des Grads des Parallelismus in einem Dataflow-Block-Artikel .

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

Hinweise

Hinweis

Die TPL-Datenflussbibliothek (System.Threading.Tasks.Dataflow-Namespace) wird nicht mit .NET ausgeliefert. Öffnen Sie zum Installieren des System.Threading.Tasks.Dataflow-Namespace in Visual Studio Ihr Projekt, wählen Sie im Menü Projekt die Option NuGet-Pakete verwalten aus, und suchen Sie online nach dem System.Threading.Tasks.Dataflow-Paket. Alternativ können Sie es mithilfe der .NET Core-CLI installieren und dazu dotnet add package System.Threading.Tasks.Dataflow ausführen.

Konstruktoren

ActionBlock<TInput>(Action<TInput>)

Initialisiert eine neue Instanz der ActionBlock<TInput>-Klasse mit der angegebenen Aktion.

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

Initialisiert eine neue Instanz der ActionBlock<TInput>-Klasse mit der angegebenen Aktion und den Konfigurationsoptionen.

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

Initialisiert eine neue Instanz der ActionBlock<TInput>-Klasse mit der angegebenen Aktion.

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

Initialisiert eine neue Instanz der ActionBlock<TInput>-Klasse mit der angegebenen Aktion und den Konfigurationsoptionen.

Eigenschaften

Completion

Ruft ein Task-Objekt ab, das den asynchronen Vorgang und die Beendigung des Datenflussblocks darstellt.

InputCount

Ruft die Anzahl der Eingabeelemente ab, die darauf warten, von diesem Block verarbeitet zu werden.

Methoden

Complete()

Signalisiert dem Datenflussblock, dass er keine weiteren Nachrichten mehr akzeptieren oder erstellen und keine weiteren hinausgeschobenen Nachrichten mehr verarbeiten soll.

Equals(Object)

Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.

(Geerbt von Object)
GetHashCode()

Fungiert als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft den Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
Post(TInput)

Stellt ein Element in den Zieldatenflussblock.

ToString()

Gibt eine Zeichenfolge zurück, die den formatierten Namen dieser IDataflowBlock-Instanz darstellt.

Explizite Schnittstellenimplementierungen

IDataflowBlock.Fault(Exception)

Veranlasst den Datenflussblock, in einem fehlgeschlagenen Zustand abzuschließen.

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

Gibt dem Datenflussblock eine Meldung und gibt ihm die Möglichkeit, die Meldung zu verarbeiten oder hinauszuschieben.

Erweiterungsmethoden

AsObserver<TInput>(ITargetBlock<TInput>)

Erstellt eine neue IObserver<T>-Abstraktion über ITargetBlock<TInput>.

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

Fügt dem ITargetBlock<TInput> ein Element hinzu.

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

Gewährt dem Zielmeldungsblock asynchron eine Meldung an und ermöglicht einen Aufschub.

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

Gewährt dem Zielmeldungsblock asynchron eine Meldung an und ermöglicht einen Aufschub.

Gilt für