共用方式為


ConcurrentExclusiveSchedulerPair 類別

定義

提供工作排程器,以協調執行工作,並確保並行工作可以同時執行,而獨佔式工作永遠不可能。

public ref class ConcurrentExclusiveSchedulerPair
public class ConcurrentExclusiveSchedulerPair
type ConcurrentExclusiveSchedulerPair = class
Public Class ConcurrentExclusiveSchedulerPair
繼承
ConcurrentExclusiveSchedulerPair

範例

下列範例顯示 類別的使用 ConcurrentExclusiveSchedulerPair 。 讀取器會在排程器的並行部分執行。 寫入器會在排程器的獨佔部分上執行。 此程式碼範例是 如何:在資料流程區塊中指定工作排程器 一文中提供之較大範例的一部分。

// 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 類別的新執行個體。

ConcurrentExclusiveSchedulerPair(TaskScheduler)

初始化 ConcurrentExclusiveSchedulerPair 類別的新執行個體,這個執行個體會以指定的排程器為目標。

ConcurrentExclusiveSchedulerPair(TaskScheduler, Int32)

初始化 ConcurrentExclusiveSchedulerPair 類別的新執行個體,這個執行個體以具有最大並行層級的指定排程器為目標。

ConcurrentExclusiveSchedulerPair(TaskScheduler, Int32, Int32)

初始化 ConcurrentExclusiveSchedulerPair 類別的新執行個體,這個執行個體以指定的排程器為目標,其具有最大並行層級與可當做一個單位來進行處理的排定工作最大數目。

屬性

Completion

取得 Task,當排程器已完成處理時,它就會完成。

ConcurrentScheduler

取得 TaskScheduler,這個物件可以用來排程工作至此組,而這些工作可能會與其他工作同時在此組上執行。

ExclusiveScheduler

取得 TaskScheduler,這個物件可以用來排程工作至此組,相對於其他工作,這些工作必須以獨佔方式在此組上執行。

方法

Complete()

通知排程器配對,它不應該接受任何更多的工作。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於