ActionBlock<TInput> Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Fornisce un blocco di flussi di dati che richiama un delegato fornito Action<T> per ogni elemento dati ricevuto.
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)
Parametri di tipo
- TInput
Tipo di dati su cui opera.ActionBlock<TInput>
- Ereditarietà
-
ActionBlock<TInput>
- Implementazioni
Esempio
Nell'esempio seguente viene illustrato l'uso della ActionBlock<TInput> classe per eseguire diversi calcoli usando blocchi di flussi di dati e restituisce il tempo trascorso necessario per eseguire i calcoli. Questo esempio di codice fa parte di un esempio più ampio fornito per l'articolo Procedura: Specificare il grado di parallelismo in un blocco di flussi di dati .
// 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
Costruttori
| Nome | Descrizione |
|---|---|
| ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions) |
Inizializza una nuova istanza della ActionBlock<TInput> classe con le opzioni di configurazione e azione specificate. |
| ActionBlock<TInput>(Action<TInput>) |
Inizializza una nuova istanza della ActionBlock<TInput> classe con l'azione specificata. |
| ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions) |
Inizializza una nuova istanza della ActionBlock<TInput> classe con le opzioni di configurazione e azione specificate. |
| ActionBlock<TInput>(Func<TInput,Task>) |
Inizializza una nuova istanza della ActionBlock<TInput> classe con l'azione specificata. |
Proprietà
| Nome | Descrizione |
|---|---|
| Completion |
Ottiene un Task oggetto che rappresenta l'operazione asincrona e il completamento del blocco del flusso di dati. |
| InputCount |
Ottiene il numero di elementi di input in attesa di essere elaborati da questo blocco. |
Metodi
| Nome | Descrizione |
|---|---|
| Complete() |
Segnala al blocco del flusso di dati che non deve accettare o produrre altri messaggi e non deve utilizzare altri messaggi posticipati. |
| Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente. (Ereditato da Object) |
| GetHashCode() |
Funge da funzione hash predefinita. (Ereditato da Object) |
| GetType() |
Ottiene il Type dell'istanza corrente. (Ereditato da Object) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| Post(TInput) |
Inserisce un elemento nel blocco del flusso di dati di destinazione. |
| ToString() |
Restituisce una stringa che rappresenta il nome formattato di questa IDataflowBlock istanza. |
Implementazioni dell'interfaccia esplicita
| Nome | Descrizione |
|---|---|
| IDataflowBlock.Fault(Exception) |
Fa sì che il blocco del flusso di dati venga completato in uno stato di errore. |
| ITargetBlock<TInput>.OfferMessage(DataflowMessageHeader, TInput, ISourceBlock<TInput>, Boolean) |
Offre un messaggio al blocco del flusso di dati e offre la possibilità di utilizzare o posticipare il messaggio. |
Metodi di estensione
| Nome | Descrizione |
|---|---|
| AsObserver<TInput>(ITargetBlock<TInput>) |
Crea una nuova IObserver<T> astrazione su ITargetBlock<TInput>. |
| Post<TInput>(ITargetBlock<TInput>, TInput) |
Inserisce un elemento nell'oggetto ITargetBlock<TInput>. |
| SendAsync<TInput>(ITargetBlock<TInput>, TInput, CancellationToken) |
Offre in modo asincrono un messaggio al blocco di messaggi di destinazione, consentendo il posticipo. |
| SendAsync<TInput>(ITargetBlock<TInput>, TInput) |
Offre in modo asincrono un messaggio al blocco di messaggi di destinazione, consentendo il posticipo. |