ActionBlock<TInput> 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
ActionBlock<TInput>(Action<TInput>) |
使用指定的操作初始化 ActionBlock<TInput> 类的新实例。 |
ActionBlock<TInput>(Func<TInput,Task>) |
使用指定的操作初始化 ActionBlock<TInput> 类的新实例。 |
ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions) |
初始化指定操作和配置选项的 ActionBlock<TInput> 类的新实例。 |
ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions) |
初始化指定操作和配置选项的 ActionBlock<TInput> 类的新实例。 |
ActionBlock<TInput>(Action<TInput>)
使用指定的操作初始化 ActionBlock<TInput> 类的新实例。
public:
ActionBlock(Action<TInput> ^ action);
public ActionBlock (Action<TInput> action);
new System.Threading.Tasks.Dataflow.ActionBlock<'Input> : Action<'Input> -> System.Threading.Tasks.Dataflow.ActionBlock<'Input>
Public Sub New (action As Action(Of TInput))
参数
- action
- Action<TInput>
使用接收的每个数据元素调用的操作。
例外
action
为 null
。
适用于
ActionBlock<TInput>(Func<TInput,Task>)
使用指定的操作初始化 ActionBlock<TInput> 类的新实例。
public:
ActionBlock(Func<TInput, System::Threading::Tasks::Task ^> ^ action);
public ActionBlock (Func<TInput,System.Threading.Tasks.Task> action);
new System.Threading.Tasks.Dataflow.ActionBlock<'Input> : Func<'Input, System.Threading.Tasks.Task> -> System.Threading.Tasks.Dataflow.ActionBlock<'Input>
Public Sub New (action As Func(Of TInput, Task))
参数
例外
action
为 null
。
适用于
ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions)
初始化指定操作和配置选项的 ActionBlock<TInput> 类的新实例。
public:
ActionBlock(Action<TInput> ^ action, System::Threading::Tasks::Dataflow::ExecutionDataflowBlockOptions ^ dataflowBlockOptions);
public ActionBlock (Action<TInput> action, System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions dataflowBlockOptions);
new System.Threading.Tasks.Dataflow.ActionBlock<'Input> : Action<'Input> * System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions -> System.Threading.Tasks.Dataflow.ActionBlock<'Input>
Public Sub New (action As Action(Of TInput), dataflowBlockOptions As ExecutionDataflowBlockOptions)
参数
- action
- Action<TInput>
使用接收的每个数据元素调用的操作。
- dataflowBlockOptions
- ExecutionDataflowBlockOptions
用来配置此 ActionBlock<TInput>的选项。
例外
示例
以下示例演示如何使用 ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions) 构造函数创建新 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>(Func<TInput,Task>, ExecutionDataflowBlockOptions)
初始化指定操作和配置选项的 ActionBlock<TInput> 类的新实例。
public:
ActionBlock(Func<TInput, System::Threading::Tasks::Task ^> ^ action, System::Threading::Tasks::Dataflow::ExecutionDataflowBlockOptions ^ dataflowBlockOptions);
public ActionBlock (Func<TInput,System.Threading.Tasks.Task> action, System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions dataflowBlockOptions);
new System.Threading.Tasks.Dataflow.ActionBlock<'Input> : Func<'Input, System.Threading.Tasks.Task> * System.Threading.Tasks.Dataflow.ExecutionDataflowBlockOptions -> System.Threading.Tasks.Dataflow.ActionBlock<'Input>
Public Sub New (action As Func(Of TInput, Task), dataflowBlockOptions As ExecutionDataflowBlockOptions)
参数
- dataflowBlockOptions
- ExecutionDataflowBlockOptions
用来配置此 ActionBlock<TInput>的选项。