ActionBlock<TInput> Klasse

Definition

Stellt einen Datenflussblock bereit, der einen bereitgestellten Action<T> Delegaten für jedes 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 dies ActionBlock<TInput> ausgeführt wird.

Vererbung
ActionBlock<TInput>
Implementiert

Beispiele

Das folgende Beispiel zeigt die Verwendung der ActionBlock<TInput> Klasse zum Ausführen mehrerer Berechnungen mithilfe von Datenflussblöcken und gibt die verstrichene Zeit zurück, die zum Ausführen der Berechnungen erforderlich ist. Dieses Codebeispiel ist Teil eines größeren Beispiels für das How to: Specify the Degree of Parallelism in a Dataflow Block article.

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

Note

Die TPL Dataflow Library (der System.Threading.Tasks.Dataflow Namespace) wird nicht mit .NET verteilt. Um den System.Threading.Tasks.Dataflow Namespace in Visual Studio zu installieren, öffnen Sie Ihr Projekt, wählen Sie "NuGet-Pakete verwalten " im Menü "Projekt " aus, und suchen Sie online nach dem System.Threading.Tasks.Dataflow Paket. Führen Sie alternativ das .NET Core CLI aus, um es zu installieren, indem Sie dotnet add package System.Threading.Tasks.Dataflow ausführen.

Konstruktoren

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

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

ActionBlock<TInput>(Action<TInput>)

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 den angegebenen Aktions- und Konfigurationsoptionen.

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

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

Eigenschaften

Name Beschreibung
Completion

Ruft ein Task Objekt ab, das den asynchronen Vorgang und den Abschluss des Datenflussblocks darstellt.

InputCount

Ruft die Anzahl der Eingabeelemente ab, die auf die Verarbeitung durch diesen Block warten.

Methoden

Name Beschreibung
Complete()

Signalisiert den Datenflussblock, dass er keine weiteren Nachrichten akzeptieren oder produzieren sollte und keine verschobenen Nachrichten mehr verbrauchen sollte.

Equals(Object)

Bestimmt, ob das angegebene Objekt dem aktuellen Objekt entspricht.

(Geerbt von Object)
GetHashCode()

Dient als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft die Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()

Erstellt eine flache Kopie der aktuellen Object.

(Geerbt von Object)
Post(TInput)

Stellt ein Element im Zieldatenflussblock bereit.

ToString()

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

Explizite Schnittstellenimplementierungen

Name Beschreibung
IDataflowBlock.Fault(Exception)

Bewirkt, dass der Datenflussblock in einem fehlerhaften Zustand abgeschlossen wird.

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

Bietet eine Nachricht an den Datenflussblock und gibt ihnen die Möglichkeit, die Nachricht zu nutzen oder zu verschieben.

Erweiterungsmethoden

Name Beschreibung
AsObserver<TInput>(ITargetBlock<TInput>)

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

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

Stellt ein Element in das ITargetBlock<TInput>.

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

Bietet asynchron eine Nachricht an den Zielnachrichtenblock, sodass eine Verschiebung möglich ist.

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

Bietet asynchron eine Nachricht an den Zielnachrichtenblock, sodass eine Verschiebung möglich ist.

Gilt für: