ActionBlock<TInput> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供一个数据流块,该块为收到的每个数据元素调用提供的 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
构造函数
| 名称 | 说明 |
|---|---|
| ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions) |
使用指定的操作和配置选项初始化类的新实例 ActionBlock<TInput> 。 |
| ActionBlock<TInput>(Action<TInput>) |
使用指定的操作初始化类的新实例 ActionBlock<TInput> 。 |
| ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions) |
使用指定的操作和配置选项初始化类的新实例 ActionBlock<TInput> 。 |
| ActionBlock<TInput>(Func<TInput,Task>) |
使用指定的操作初始化类的新实例 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, CancellationToken) |
以异步方式向目标消息块提供消息,从而允许推迟。 |
| SendAsync<TInput>(ITargetBlock<TInput>, TInput) |
以异步方式向目标消息块提供消息,从而允许推迟。 |