ActionBlock<TInput> Constructors

Definition

Overloads

ActionBlock<TInput>(Action<TInput>)

Initializes a new instance of the ActionBlock<TInput> class with the specified action.

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

Initializes a new instance of the ActionBlock<TInput> class with the specified action.

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

Initializes a new instance of the ActionBlock<TInput> class with the specified action and configuration options.

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

Initializes a new instance of the ActionBlock<TInput> class with the specified action and configuration options.

ActionBlock<TInput>(Action<TInput>)

Source:
ActionBlock.cs
Source:
ActionBlock.cs
Source:
ActionBlock.cs
Source:
ActionBlock.cs

Initializes a new instance of the ActionBlock<TInput> class with the specified action.

public ActionBlock (Action<TInput> action);

Parameters

action
Action<TInput>

The action to invoke with each data element received.

Exceptions

action is null.

Applies to

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)
UWP 10.0

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

Source:
ActionBlock.cs
Source:
ActionBlock.cs
Source:
ActionBlock.cs
Source:
ActionBlock.cs

Initializes a new instance of the ActionBlock<TInput> class with the specified action.

public ActionBlock (Func<TInput,System.Threading.Tasks.Task> action);

Parameters

action
Func<TInput,Task>

The action to invoke with each data element received.

Exceptions

action is null.

Applies to

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)
UWP 10.0

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

Source:
ActionBlock.cs
Source:
ActionBlock.cs
Source:
ActionBlock.cs
Source:
ActionBlock.cs

Initializes a new instance of the ActionBlock<TInput> class with the specified action and configuration options.

public ActionBlock (Action<TInput> action, System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions dataflowBlockOptions);

Parameters

action
Action<TInput>

The action to invoke with each data element received.

dataflowBlockOptions
ExecutionDataflowBlockOptions

The options with which to configure this ActionBlock<TInput>.

Exceptions

action is null.

-or-

dataflowBlockOptions is null.

Examples

The following example shows how to use the ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions) constructor to create a new ActionBlock<TInput> object. This code example is part of a larger example provided for the How to: Specify the Degree of Parallelism in a Dataflow Block topic.

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

Applies to

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)
UWP 10.0

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

Source:
ActionBlock.cs
Source:
ActionBlock.cs
Source:
ActionBlock.cs
Source:
ActionBlock.cs

Initializes a new instance of the ActionBlock<TInput> class with the specified action and configuration options.

public ActionBlock (Func<TInput,System.Threading.Tasks.Task> action, System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions dataflowBlockOptions);

Parameters

action
Func<TInput,Task>

The action to invoke with each data element received.

dataflowBlockOptions
ExecutionDataflowBlockOptions

The options with which to configure this ActionBlock<TInput>.

Exceptions

action is null.

-or-

dataflowBlockOptions is null.

Applies to

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)
UWP 10.0