ActionBlock<TInput> 类

定义

提供数据流块,其调用为每个接收的数据提供的 Action<T> 委托。

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)

类型参数

TInput

ActionBlock<TInput> 操作的数据类型。

继承
ActionBlock<TInput>
实现

示例

以下示例演示如何使用 ActionBlock<TInput> 类使用数据流块执行多个计算,并返回执行计算所需的已用时间。 此代码示例是 有关如何:在数据流块文章中指定并行度的大型示例的一 部分。

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

注解

备注

TPL 数据流库(System.Threading.Tasks.Dataflow 命名空间)不随 .NET 一起分发。 若要在 Visual Studio 中安装 System.Threading.Tasks.Dataflow 命名空间,请打开项目,选择“项目”菜单中的“管理 NuGet 包”,再在线搜索 System.Threading.Tasks.Dataflow 包。 或者,若要使用 .NET Core CLI 进行安装,请运行 dotnet add package System.Threading.Tasks.Dataflow

构造函数

ActionBlock<TInput>(Action<TInput>)

使用指定的操作初始化 ActionBlock<TInput> 类的新实例。

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

初始化指定操作和配置选项的 ActionBlock<TInput> 类的新实例。

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

使用指定的操作初始化 ActionBlock<TInput> 类的新实例。

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

初始化指定操作和配置选项的 ActionBlock<TInput> 类的新实例。

属性

Completion

获取 Task 对象,该对象表示数据流块的异步操作和完成。

InputCount

获取等待被此代码块处理的输入项的数目。

方法

Complete()

通知数据流块,告知其不应再接受或生成更多消息也不应再使用更多的延迟消息。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Post(TInput)

将项目发送到目标数据流块。

ToString()

返回一个字符串,该字符串表示此 IDataflowBlock 实例的格式化名称。

显式接口实现

IDataflowBlock.Fault(Exception)

导致数据流块在出错的状态下完成。

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

提供信息至数据流块,并提供使用或延迟信息的机会。

扩展方法

AsObserver<TInput>(ITargetBlock<TInput>)

通过 IObserver<T> 创建新的 ITargetBlock<TInput> 抽象。

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

将项发送到 ITargetBlock<TInput> 中。

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

为目标消息块异步提供消息,允许延期。

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

为目标消息块异步提供消息,允许延期。

适用于