Dispatcher 類別

定義

提供管理執行緒之工作項目佇列的服務。Provides services for managing the queue of work items for a thread.

public ref class Dispatcher sealed
public sealed class Dispatcher
type Dispatcher = class
Public NotInheritable Class Dispatcher
繼承
Dispatcher

範例

下列範例示範如何將作業放在上 DispatcherThe following example shows how to place an operation onto a Dispatcher. 如需此範例的完整原始程式碼,請參閱 具有長時間執行計算範例的單一執行緒應用程式For the full source code of this example, see Single-Threaded Application with Long-Running Calculation Sample.

首先,會建立不接受引數的委派。First, a delegate is created that accepts no arguments.

public delegate void NextPrimeDelegate();
Public Delegate Sub NextPrimeDelegate()

接下來 BeginInvoke(DispatcherPriority, Delegate) 會呼叫。Next, BeginInvoke(DispatcherPriority, Delegate) is called. 這個呼叫 BeginInvoke(DispatcherPriority, Delegate) 會採用兩個參數:優先順序(設為 DispatcherPriority.Normal )和回呼(透過委派的實例傳遞) NextPrimeDelegateThis call to BeginInvoke(DispatcherPriority, Delegate) takes two parameters: the priority, which is set to DispatcherPriority.Normal, and the callback, which is passed in through an instance of the delegate NextPrimeDelegate.

startStopButton.Dispatcher.BeginInvoke(
    DispatcherPriority.Normal,
    new NextPrimeDelegate(CheckNextNumber));
startStopButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New NextPrimeDelegate(AddressOf CheckNextNumber))

備註

Dispatcher 針對特定執行緒維護工作專案的優先順序佇列。The Dispatcher maintains a prioritized queue of work items for a specific thread.

當建立 Dispatcher 線上程上時,它會變成唯一 Dispatcher 可與執行緒相關聯的,即使 Dispatcher 是關閉的。When a Dispatcher is created on a thread, it becomes the only Dispatcher that can be associated with the thread, even if the Dispatcher is shut down.

如果您嘗試取得 CurrentDispatcher 目前線程的,且 Dispatcher 沒有與執行緒相關聯,則 Dispatcher 會建立。If you attempt to get the CurrentDispatcher for the current thread and a Dispatcher is not associated with the thread, a Dispatcher will be created. Dispatcher當您建立時,也會建立 DispatcherObjectA Dispatcher is also created when you create a DispatcherObject. 如果您 Dispatcher 在背景執行緒上建立,請務必先關閉發送器,再結束執行緒。If you create a Dispatcher on a background thread, be sure to shut down the dispatcher before exiting the thread.

如果 Dispatcher 已關閉,就無法重新開機。If a Dispatcher is shut down, it cannot be restarted.

在 WPF 中, DispatcherObject 只能由 Dispatcher 與相關聯的來存取。In WPF, a DispatcherObject can only be accessed by the Dispatcher it is associated with. 例如,背景執行緒無法更新 Button 與執行緒上相關聯之的內容 Dispatcher UIUIFor example, a background thread cannot update the contents of a Button that is associated with the Dispatcher on the UIUI thread. 為了讓背景執行緒存取的 Content 屬性 Button ,背景執行緒必須將工作委派給 Dispatcher 與執行緒相關聯的 UIUIIn order for the background thread to access the Content property of the Button, the background thread must delegate the work to the Dispatcher associated with the UIUI thread. 您可以使用或來完成這項作業 Invoke BeginInvokeThis is accomplished by using either Invoke or BeginInvoke. Invoke 是同步的,而且 BeginInvoke 是非同步。Invoke is synchronous and BeginInvoke is asynchronous. 作業會加入至指定之的佇列 Dispatcher DispatcherPriorityThe operation is added to the queue of the Dispatcher at the specified DispatcherPriority.

如果 BeginInvoke 在已關閉的上呼叫 Dispatcher ,則傳回的 status 屬性 DispatcherOperation 會設定為 AbortedIf BeginInvoke is called on a Dispatcher that has shut down, the status property of the returned DispatcherOperation is set to Aborted.

除了之外,的所有方法 Dispatcher DisableProcessing 都是無限制執行緒。All of the methods on Dispatcher, with the exception of DisableProcessing, are free-threaded.

衍生自的物件 DispatcherObject 具有線程親和性。Objects that derive from DispatcherObject have thread affinity.

在凍結時,衍生自的物件 Freezable 是無限制執行緒。Objects that derive from Freezable are free-threaded when they are frozen. 如需詳細資訊,請參閱 Freezable 物件概觀For more information, see Freezable Objects Overview.

屬性

CurrentDispatcher

取得目前執行之執行緒的 Dispatcher 並建立新的 Dispatcher (如果尚未與執行緒產生關聯)。Gets the Dispatcher for the thread currently executing and creates a new Dispatcher if one is not already associated with the thread.

HasShutdownFinished

判斷 Dispatcher 是否已完成關閉程序。Determines whether the Dispatcher has finished shutting down.

HasShutdownStarted

判斷 Dispatcher 是否已關閉。Determines whether the Dispatcher is shutting down.

Hooks

取得提供 Dispatcher 額外事件資訊的攔截集合。Gets the collection of hooks that provide additional event information about the Dispatcher.

Thread

取得與這個 Dispatcher 關聯的執行緒。Gets the thread this Dispatcher is associated with.

方法

BeginInvoke(Delegate, DispatcherPriority, Object[])

在其中有建立 Dispatcher 的執行緒上,依據指定的優先順序,使用指定的引數非同步地執行指定的委派。Executes the specified delegate asynchronously with the specified arguments, at the specified priority, on the thread that the Dispatcher was created on.

BeginInvoke(Delegate, Object[])

在其中有建立 Dispatcher 的執行緒上,使用指定的引數非同步地執行指定的委派。Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on.

BeginInvoke(DispatcherPriority, Delegate)

Dispatcher 關聯的執行緒上使用指定優先權以非同步方式執行指定的委派。Executes the specified delegate asynchronously at the specified priority on the thread the Dispatcher is associated with.

BeginInvoke(DispatcherPriority, Delegate, Object)

Dispatcher 關聯的執行緒上使用指定優先權和指定引數以非同步方式執行指定的委派。Executes the specified delegate asynchronously at the specified priority and with the specified argument on the thread the Dispatcher is associated with.

BeginInvoke(DispatcherPriority, Delegate, Object, Object[])

Dispatcher 關聯的執行緒上使用指定優先權和指定引數陣列以非同步方式執行指定的委派。Executes the specified delegate asynchronously at the specified priority and with the specified array of arguments on the thread the Dispatcher is associated with.

BeginInvokeShutdown(DispatcherPriority)

以非同步方式初始化 Dispatcher 的關閉程序。Initiates shutdown of the Dispatcher asynchronously.

CheckAccess()

判斷呼叫的執行緒是否為與這個 Dispatcher 關聯的執行緒。Determines whether the calling thread is the thread associated with this Dispatcher.

DisableProcessing()

停用 Dispatcher 佇列的處理。Disables processing of the Dispatcher queue.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
ExitAllFrames()

會要求結束所有框架,包括巢狀框架。Requests that all frames exit, including nested frames.

FromThread(Thread)

取得指定執行緒的 DispatcherGets the Dispatcher for the specified thread.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
Invoke(Action)

在與 Action 相關聯的執行緒上,同步執行指定的 DispatcherExecutes the specified Action synchronously on the thread the Dispatcher is associated with.

Invoke(Action, DispatcherPriority)

在與 Action 相關聯的執行緒上使用指定優先權同步執行指定的 DispatcherExecutes the specified Action synchronously at the specified priority on the thread the Dispatcher is associated with.

Invoke(Action, DispatcherPriority, CancellationToken)

在與 Action 相關聯的執行緒上使用指定優先權同步執行指定的 DispatcherExecutes the specified Action synchronously at the specified priority on the thread the Dispatcher is associated with.

Invoke(Action, DispatcherPriority, CancellationToken, TimeSpan)

在與 Action 相關聯的執行緒上使用指定優先權同步執行指定的 DispatcherExecutes the specified Action synchronously at the specified priority on the thread the Dispatcher is associated with.

Invoke(Delegate, DispatcherPriority, Object[])

Dispatcher 關聯的執行緒上使用指定優先權和指定引數以同步方式執行指定的委派。Executes the specified delegate at the specified priority with the specified arguments synchronously on the thread the Dispatcher is associated with.

Invoke(Delegate, Object[])

在與 Dispatcher 相關聯的執行緒上,使用指定的引數同步地執行指定的委派。Executes the specified delegate with the specified arguments synchronously on the thread the Dispatcher is associated with.

Invoke(Delegate, TimeSpan, DispatcherPriority, Object[])

在與 Dispatcher 有關聯的執行緒上,在指定的時間範圍內依據指定優先順序,使用指定之引數同步地執行指定的委派。Executes the specified delegate within the designated time span at the specified priority with the specified arguments synchronously on the thread the Dispatcher is associated with.

Invoke(Delegate, TimeSpan, Object[])

在與 Dispatcher 有關聯的執行緒上,在指定的時間範圍內依據指定優先順序,使用指定之引數同步地執行指定的委派。Executes the specified delegate within the designated time span at the specified priority with the specified arguments synchronously on the thread the Dispatcher is associated with.

Invoke(DispatcherPriority, Delegate)

在與相關聯的執行緒上,以指定的優先權同步執行指定的委派 DispatcherExecutes the specified delegate synchronously at the specified priority on the thread that the Dispatcher is associated with.

Invoke(DispatcherPriority, Delegate, Object)

Dispatcher 關聯的執行緒上使用指定優先權和指定引數以同步方式執行指定的委派。Executes the specified delegate at the specified priority with the specified argument synchronously on the thread the Dispatcher is associated with.

Invoke(DispatcherPriority, Delegate, Object, Object[])

Dispatcher 關聯的執行緒上使用指定優先權和指定引數以同步方式執行指定的委派。Executes the specified delegate at the specified priority with the specified arguments synchronously on the thread the Dispatcher is associated with.

Invoke(DispatcherPriority, TimeSpan, Delegate)

在建立 Dispatcher 的執行緒上使用指定優先權和指定逾時值以同步方式執行指定的委派。Executes the specified delegate synchronously at the specified priority and with the specified time-out value on the thread the Dispatcher was created.

Invoke(DispatcherPriority, TimeSpan, Delegate, Object)

Dispatcher 關聯的執行緒上使用指定優先權和指定引數以同步方式執行指定的委派。Executes the specified delegate at the specified priority with the specified argument synchronously on the thread the Dispatcher is associated with.

Invoke(DispatcherPriority, TimeSpan, Delegate, Object, Object[])

Dispatcher 關聯的執行緒上使用指定優先權和指定引數以同步方式執行指定的委派。Executes the specified delegate at the specified priority with the specified arguments synchronously on the thread the Dispatcher is associated with.

Invoke<TResult>(Func<TResult>)

在與 Func<TResult> 相關聯的執行緒上,同步執行指定的 DispatcherExecutes the specified Func<TResult> synchronously on the thread the Dispatcher is associated with.

Invoke<TResult>(Func<TResult>, DispatcherPriority)

在與 Func<TResult> 相關聯的執行緒上使用指定優先權同步執行指定的 DispatcherExecutes the specified Func<TResult> synchronously at the specified priority on the thread the Dispatcher is associated with.

Invoke<TResult>(Func<TResult>, DispatcherPriority, CancellationToken)

在與 Func<TResult> 相關聯的執行緒上使用指定優先權同步執行指定的 DispatcherExecutes the specified Func<TResult> synchronously at the specified priority on the thread the Dispatcher is associated with.

Invoke<TResult>(Func<TResult>, DispatcherPriority, CancellationToken, TimeSpan)

在與 Func<TResult> 相關聯的執行緒上使用指定優先權同步執行指定的 DispatcherExecutes the specified Func<TResult> synchronously at the specified priority on the thread the Dispatcher is associated with.

InvokeAsync(Action)

在與 Action 相關聯的執行緒上,以非同步方式執行指定的 DispatcherExecutes the specified Action asynchronously on the thread the Dispatcher is associated with.

InvokeAsync(Action, DispatcherPriority)

在與 Action 相關聯的執行緒上使用指定優先權以非同步方式執行指定的 DispatcherExecutes the specified Action asynchronously at the specified priority on the thread the Dispatcher is associated with.

InvokeAsync(Action, DispatcherPriority, CancellationToken)

在與 Action 相關聯的執行緒上使用指定優先權以非同步方式執行指定的 DispatcherExecutes the specified Action asynchronously at the specified priority on the thread the Dispatcher is associated with.

InvokeAsync<TResult>(Func<TResult>)

在與 Func<TResult> 相關聯的執行緒上,以非同步方式執行指定的 DispatcherExecutes the specified Func<TResult> asynchronously on the thread the Dispatcher is associated with.

InvokeAsync<TResult>(Func<TResult>, DispatcherPriority)

在與 Func<TResult> 相關聯的執行緒上使用指定優先權以非同步方式執行指定的 DispatcherExecutes the specified Func<TResult> asynchronously at the specified priority on the thread the Dispatcher is associated with.

InvokeAsync<TResult>(Func<TResult>, DispatcherPriority, CancellationToken)

在與 Func<TResult> 相關聯的執行緒上使用指定優先權以非同步方式執行指定的 DispatcherExecutes the specified Func<TResult> asynchronously at the specified priority on the thread the Dispatcher is associated with.

InvokeShutdown()

以同步方式初始化 Dispatcher 的關閉程序。Initiates the shutdown process of the Dispatcher synchronously.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
PushFrame(DispatcherFrame)

會進入執行迴圈。Enters an execute loop.

Run()

推入 Dispatcher 事件佇列上的主執行框架。Pushes the main execution frame on the event queue of the Dispatcher.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
ValidatePriority(DispatcherPriority, String)

判斷指定的 DispatcherPriority 是否為有效的優先權。Determines whether the specified DispatcherPriority is a valid priority.

VerifyAccess()

判斷呼叫的執行是否可以存取這個 DispatcherDetermines whether the calling thread has access to this Dispatcher.

Yield()

建立 awaitable 物件,這個物件會以非同步方式讓出控制權給目前的發送器,並提供機會給發送器以處理其他事件。Creates an awaitable object that asynchronously yields control back to the current dispatcher and provides an opportunity for the dispatcher to process other events.

Yield(DispatcherPriority)

建立 awaitable 物件,這個物件會以非同步方式讓出控制權給目前的發送器,並提供機會給發送器以處理其他事件。Creates an awaitable object that asynchronously yields control back to the current dispatcher and provides an opportunity for the dispatcher to process other events. 在控制權回到等候此方法之結果的程式碼時進行的工作會以指定的優先權來排定。The work that occurs when control returns to the code awaiting the result of this method is scheduled with the specified priority.

事件

ShutdownFinished

發生於 Dispatcher 完成關閉時。Occurs when the Dispatcher finishes shutting down.

ShutdownStarted

發生於 Dispatcher 開始關閉時。Occurs when the Dispatcher begins to shut down.

UnhandledException

發生於當擲回執行緒例外狀況且透過 InvokeBeginInvoke 執行委派期間無法攔截時。Occurs when a thread exception is thrown and uncaught during execution of a delegate by way of Invoke or BeginInvoke.

UnhandledExceptionFilter

發生於在篩選階段擲回執行緒例外狀況且透過 InvokeBeginInvoke 執行委派期間無法攔截時。Occurs when a thread exception is thrown and uncaught during execution of a delegate by way of Invoke or BeginInvoke when in the filter stage.

擴充方法

BeginInvoke(Dispatcher, Action)

在特定 Dispatcher 所建立的執行緒上,以非同步的方式,根據一般優先權執行指定的委派。Executes the specified delegate asynchronously with normal priority on the thread that the specified Dispatcher was created on.

BeginInvoke(Dispatcher, Action, DispatcherPriority)

在特定 Dispatcher 所建立的執行緒上,以非同步的方式,根據特定優先權,執行指定的委派。Executes the specified delegate asynchronously with the specified priority on the thread that the specified Dispatcher was created on.

Invoke(Dispatcher, Action)

在特定 Dispatcher 所建立的執行緒上,以同步方式,根據一般優先權,執行指定的委派。Executes the specified delegate synchronously with normal priority on the thread that the specified Dispatcher was created on.

Invoke(Dispatcher, Action, TimeSpan)

在特定 Dispatcher 所建立的執行緒上,以同步方式,執行指定的委派,並在指定逾時期間後停止執行。Executes the specified delegate synchronously on the thread that the specified Dispatcher was created on, and stops execution after the specified time-out period.

Invoke(Dispatcher, Action, TimeSpan, DispatcherPriority)

在特定 Dispatcher 所建立的執行緒上,以同步方式,根據特定優先權,執行指定的委派,並於指定逾時期間後停止執行。Executes the specified delegate synchronously with the specified priority on the thread that the specified Dispatcher was created on, and stops execution after the specified time-out period.

Invoke(Dispatcher, Action, DispatcherPriority)

在特定 Dispatcher 所建立的執行緒上,以同步方式,根據特定優先權,執行指定的委派。Executes the specified delegate synchronously with the specified priority on the thread that the specified Dispatcher was created on.

適用於

另請參閱