ConcurrentExclusiveSchedulerPair 类
定义
提供任务计划程序,其用于执行任务,同时确保并发任务可同时运行,而独占任务从不运行。Provides task schedulers that coordinate to execute tasks while ensuring that concurrent tasks may run concurrently and exclusive tasks never do.
public ref class ConcurrentExclusiveSchedulerPair
public class ConcurrentExclusiveSchedulerPair
type ConcurrentExclusiveSchedulerPair = class
Public Class ConcurrentExclusiveSchedulerPair
- 继承
-
ConcurrentExclusiveSchedulerPair
示例
下例显示了 ConcurrentExclusiveSchedulerPair 类的用法。The following example shows the use of the ConcurrentExclusiveSchedulerPair class. 读取器将在计划程序的并发部分运行。Readers run on the concurrent part of the scheduler. 编写器在计划程序的独占部分运行。The writer runs on the exclusive part of the scheduler. 此代码示例摘自为 如何:在数据流块中指定任务计划程序 提供的更大示例的一部分。This code example is part of a larger example provided for the How to: Specify a Task Scheduler in a Dataflow Block topic.
// Create a ConcurrentExclusiveSchedulerPair object.
// Readers will run on the concurrent part of the scheduler pair.
// The writer will run on the exclusive part of the scheduler pair.
var taskSchedulerPair = new ConcurrentExclusiveSchedulerPair();
// Create an ActionBlock<int> object for each reader CheckBox object.
// Each ActionBlock<int> object represents an action that can read
// from a resource in parallel to other readers.
// Specifying the concurrent part of the scheduler pair enables the
// reader to run in parallel to other actions that are managed by
// that scheduler.
var readerActions =
from checkBox in new CheckBox[] {checkBox1, checkBox2, checkBox3}
select new ActionBlock<int>(milliseconds =>
{
// Toggle the check box to the checked state.
toggleCheckBox.Post(checkBox);
// Perform the read action. For demonstration, suspend the current
// thread to simulate a lengthy read operation.
Thread.Sleep(milliseconds);
// Toggle the check box to the unchecked state.
toggleCheckBox.Post(checkBox);
},
new ExecutionDataflowBlockOptions
{
TaskScheduler = taskSchedulerPair.ConcurrentScheduler
});
// Create an ActionBlock<int> object for the writer CheckBox object.
// This ActionBlock<int> object represents an action that writes to
// a resource, but cannot run in parallel to readers.
// Specifying the exclusive part of the scheduler pair enables the
// writer to run in exclusively with respect to other actions that are
// managed by the scheduler pair.
var writerAction = new ActionBlock<int>(milliseconds =>
{
// Toggle the check box to the checked state.
toggleCheckBox.Post(checkBox4);
// Perform the write action. For demonstration, suspend the current
// thread to simulate a lengthy write operation.
Thread.Sleep(milliseconds);
// Toggle the check box to the unchecked state.
toggleCheckBox.Post(checkBox4);
},
new ExecutionDataflowBlockOptions
{
TaskScheduler = taskSchedulerPair.ExclusiveScheduler
});
// Link the broadcaster to each reader and writer block.
// The BroadcastBlock<T> class propagates values that it
// receives to all connected targets.
foreach (var readerAction in readerActions)
{
broadcaster.LinkTo(readerAction);
}
broadcaster.LinkTo(writerAction);
' Create a ConcurrentExclusiveSchedulerPair object.
' Readers will run on the concurrent part of the scheduler pair.
' The writer will run on the exclusive part of the scheduler pair.
Dim taskSchedulerPair = New ConcurrentExclusiveSchedulerPair()
' Create an ActionBlock<int> object for each reader CheckBox object.
' Each ActionBlock<int> object represents an action that can read
' from a resource in parallel to other readers.
' Specifying the concurrent part of the scheduler pair enables the
' reader to run in parallel to other actions that are managed by
' that scheduler.
Dim readerActions = From checkBox In New CheckBox() {checkBox1, checkBox2, checkBox3} _
Select New ActionBlock(Of Integer)(Sub(milliseconds)
' Toggle the check box to the checked state.
' Perform the read action. For demonstration, suspend the current
' thread to simulate a lengthy read operation.
' Toggle the check box to the unchecked state.
toggleCheckBox.Post(checkBox)
Thread.Sleep(milliseconds)
toggleCheckBox.Post(checkBox)
End Sub, New ExecutionDataflowBlockOptions With {.TaskScheduler = taskSchedulerPair.ConcurrentScheduler})
' Create an ActionBlock<int> object for the writer CheckBox object.
' This ActionBlock<int> object represents an action that writes to
' a resource, but cannot run in parallel to readers.
' Specifying the exclusive part of the scheduler pair enables the
' writer to run in exclusively with respect to other actions that are
' managed by the scheduler pair.
Dim writerAction = New ActionBlock(Of Integer)(Sub(milliseconds)
' Toggle the check box to the checked state.
' Perform the write action. For demonstration, suspend the current
' thread to simulate a lengthy write operation.
' Toggle the check box to the unchecked state.
toggleCheckBox.Post(checkBox4)
Thread.Sleep(milliseconds)
toggleCheckBox.Post(checkBox4)
End Sub, New ExecutionDataflowBlockOptions With {.TaskScheduler = taskSchedulerPair.ExclusiveScheduler})
' Link the broadcaster to each reader and writer block.
' The BroadcastBlock<T> class propagates values that it
' receives to all connected targets.
For Each readerAction In readerActions
broadcaster.LinkTo(readerAction)
Next readerAction
broadcaster.LinkTo(writerAction)
构造函数
| ConcurrentExclusiveSchedulerPair() |
初始化 ConcurrentExclusiveSchedulerPair 类的新实例。Initializes a new instance of the ConcurrentExclusiveSchedulerPair class. |
| ConcurrentExclusiveSchedulerPair(TaskScheduler) |
用指定的计划程序目标初始化 ConcurrentExclusiveSchedulerPair 类的新实例。Initializes a new instance of the ConcurrentExclusiveSchedulerPair class that targets the specified scheduler. |
| ConcurrentExclusiveSchedulerPair(TaskScheduler, Int32) |
初始化针对最大并发级别的指定计划程序 ConcurrentExclusiveSchedulerPair 类的新实例。Initializes a new instance of the ConcurrentExclusiveSchedulerPair class that targets the specified scheduler with a maximum concurrency level. |
| ConcurrentExclusiveSchedulerPair(TaskScheduler, Int32, Int32) |
初始化 ConcurrentExclusiveSchedulerPair 类的新实例,该实例针对最大并发级别和最大计划任务数的指定计划程序,并可能作为一个单元来处理。Initializes a new instance of the ConcurrentExclusiveSchedulerPair class that targets the specified scheduler with a maximum concurrency level and a maximum number of scheduled tasks that may be processed as a unit. |
属性
| Completion |
当调度程序已经完成处理过程时,获取将要完成的 Task。Gets a Task that will complete when the scheduler has completed processing. |
| ConcurrentScheduler |
获取可用于调度任务到此添加线对的 TaskScheduler,其中的线对可以与此线对上的其他任务同时运行。Gets a TaskScheduler that can be used to schedule tasks to this pair that may run concurrently with other tasks on this pair. |
| ExclusiveScheduler |
获取可用于调度任务到此添加线对的 TaskScheduler,其中的线对必须独占地运行,不考虑此线对上的其他任务。Gets a TaskScheduler that can be used to schedule tasks to this pair that must run exclusively with regards to other tasks on this pair. |
方法
| Complete() |
通知不能接受更多任务的计划程序对。Informs the scheduler pair that it should not accept any more tasks. |
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |