Dispatcher Dispatcher Dispatcher Dispatcher Class

定义

提供用于管理线程工作项队列的服务。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
继承
DispatcherDispatcherDispatcherDispatcher

示例

下面的示例演示如何在Dispatcher上放置操作。The 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的优先级,以及通过委托NextPrimeDelegate的实例传入的回调。This 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关闭,它也将变为可与线程关联的唯一DispatcherDispatcherWhen 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.

如果尝试获取当前线程的CurrentDispatcherDispatcher并且没有与线程关联,则将创建一个DispatcherIf you attempt to get the CurrentDispatcher for the current thread and a Dispatcher is not associated with the thread, a Dispatcher will be created. DispatcherDispatcherObject创建时,还会创建一个。A 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.

WPFWPFDispatcherObjectDispatcher只能由与相关联的来访问。In WPFWPF, a DispatcherObject can only be accessed by the Dispatcher it is associated with. 例如,后台线程无法Button DispatcherUIUI线程上更新与关联的的内容。For example, a background thread cannot update the contents of a Button that is associated with the Dispatcher on the UIUI thread. 为了让后台线程Content访问的Button属性,后台线程必须将工作委托UIUI给与线程关联的DispatcherIn 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通过使用或BeginInvoke来完成。This is accomplished by using either Invoke or BeginInvoke. Invoke是同步的BeginInvoke ,并且是异步的。Invoke is synchronous and BeginInvoke is asynchronous. 操作将添加到指定Dispatcher DispatcherPriority的的队列中。The operation is added to the queue of the Dispatcher at the specified DispatcherPriority.

如果BeginInvoke DispatcherOperation对已关闭的调用,则返回的状态属性设置为AbortedDispatcherIf 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 CurrentDispatcher CurrentDispatcher CurrentDispatcher

获取当前正在执行的线程的 Dispatcher,并在该线程没有关联的调度程序时创建一个新的 DispatcherGets the Dispatcher for the thread currently executing and creates a new Dispatcher if one is not already associated with the thread.

HasShutdownFinished HasShutdownFinished HasShutdownFinished HasShutdownFinished

确定 Dispatcher 是否已经完成关闭。Determines whether the Dispatcher has finished shutting down.

HasShutdownStarted HasShutdownStarted HasShutdownStarted HasShutdownStarted

确定 Dispatcher 是否正在关闭。Determines whether the Dispatcher is shutting down.

Hooks Hooks Hooks Hooks

获取提供有关 Dispatcher 的其他事件信息的挂钩集合。Gets the collection of hooks that provide additional event information about the Dispatcher.

Thread Thread Thread Thread

获取与此 Dispatcher 关联的线程。Gets the thread this Dispatcher is associated with.

方法

BeginInvoke(Delegate, DispatcherPriority, Object[]) BeginInvoke(Delegate, DispatcherPriority, Object[]) BeginInvoke(Delegate, DispatcherPriority, Object[]) 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[]) BeginInvoke(Delegate, Object[]) BeginInvoke(Delegate, Object[]) 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) BeginInvoke(DispatcherPriority, Delegate) BeginInvoke(DispatcherPriority, Delegate) 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) BeginInvoke(DispatcherPriority, Delegate, Object) BeginInvoke(DispatcherPriority, Delegate, Object) 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[]) BeginInvoke(DispatcherPriority, Delegate, Object, Object[]) BeginInvoke(DispatcherPriority, Delegate, Object, Object[]) 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) BeginInvokeShutdown(DispatcherPriority) BeginInvokeShutdown(DispatcherPriority) BeginInvokeShutdown(DispatcherPriority)

异步启动 Dispatcher 的关闭。Initiates shutdown of the Dispatcher asynchronously.

CheckAccess() CheckAccess() CheckAccess() CheckAccess()

确定调用线程是否为与此 Dispatcher 关联的线程。Determines whether the calling thread is the thread associated with this Dispatcher.

DisableProcessing() DisableProcessing() DisableProcessing() DisableProcessing()

禁用对 Dispatcher 队列的处理。Disables processing of the Dispatcher queue.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
ExitAllFrames() ExitAllFrames() ExitAllFrames() ExitAllFrames()

请求退出所有帧,包括嵌套的帧。Requests that all frames exit, including nested frames.

FromThread(Thread) FromThread(Thread) FromThread(Thread) FromThread(Thread)

获取指定线程的 DispatcherGets the Dispatcher for the specified thread.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
Invoke(Action) Invoke(Action) Invoke(Action) Invoke(Action)

在与 Action 关联的线程上同步执行指定的 DispatcherExecutes the specified Action synchronously on the thread the Dispatcher is associated with.

Invoke(Action, DispatcherPriority) Invoke(Action, DispatcherPriority) Invoke(Action, DispatcherPriority) 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) Invoke(Action, DispatcherPriority, CancellationToken) Invoke(Action, DispatcherPriority, CancellationToken) 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) Invoke(Action, DispatcherPriority, CancellationToken, TimeSpan) Invoke(Action, DispatcherPriority, CancellationToken, TimeSpan) 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[]) Invoke(Delegate, DispatcherPriority, Object[]) Invoke(Delegate, DispatcherPriority, Object[]) 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[]) Invoke(Delegate, Object[]) Invoke(Delegate, Object[]) 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[]) Invoke(Delegate, TimeSpan, DispatcherPriority, Object[]) Invoke(Delegate, TimeSpan, DispatcherPriority, Object[]) 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[]) Invoke(Delegate, TimeSpan, Object[]) Invoke(Delegate, TimeSpan, Object[]) 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) Invoke(DispatcherPriority, Delegate) Invoke(DispatcherPriority, Delegate) Invoke(DispatcherPriority, Delegate)

按指定的优先级在与 Dispatcher 关联的线程上同步执行指定的委托。Executes the specified delegate synchronously at the specified priority on the thread on which the Dispatcher is associated with.

Invoke(DispatcherPriority, Delegate, Object) Invoke(DispatcherPriority, Delegate, Object) Invoke(DispatcherPriority, Delegate, Object) 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[]) Invoke(DispatcherPriority, Delegate, Object, Object[]) Invoke(DispatcherPriority, Delegate, Object, Object[]) 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) Invoke(DispatcherPriority, TimeSpan, Delegate) Invoke(DispatcherPriority, TimeSpan, Delegate) 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) Invoke(DispatcherPriority, TimeSpan, Delegate, Object) Invoke(DispatcherPriority, TimeSpan, Delegate, Object) 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[]) Invoke(DispatcherPriority, TimeSpan, Delegate, Object, Object[]) Invoke(DispatcherPriority, TimeSpan, Delegate, Object, Object[]) 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>) Invoke<TResult>(Func<TResult>) Invoke<TResult>(Func<TResult>) 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) Invoke<TResult>(Func<TResult>, DispatcherPriority) Invoke<TResult>(Func<TResult>, DispatcherPriority) 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) Invoke<TResult>(Func<TResult>, DispatcherPriority, CancellationToken) Invoke<TResult>(Func<TResult>, DispatcherPriority, CancellationToken) 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) Invoke<TResult>(Func<TResult>, DispatcherPriority, CancellationToken, TimeSpan) Invoke<TResult>(Func<TResult>, DispatcherPriority, CancellationToken, TimeSpan) 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) InvokeAsync(Action) InvokeAsync(Action) InvokeAsync(Action)

在与 Action 关联的线程上异步执行指定的 DispatcherExecutes the specified Action asynchronously on the thread the Dispatcher is associated with.

InvokeAsync(Action, DispatcherPriority) InvokeAsync(Action, DispatcherPriority) InvokeAsync(Action, DispatcherPriority) 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) InvokeAsync(Action, DispatcherPriority, CancellationToken) InvokeAsync(Action, DispatcherPriority, CancellationToken) 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>) InvokeAsync<TResult>(Func<TResult>) InvokeAsync<TResult>(Func<TResult>) 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) InvokeAsync<TResult>(Func<TResult>, DispatcherPriority) InvokeAsync<TResult>(Func<TResult>, DispatcherPriority) 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) InvokeAsync<TResult>(Func<TResult>, DispatcherPriority, CancellationToken) InvokeAsync<TResult>(Func<TResult>, DispatcherPriority, CancellationToken) 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() InvokeShutdown() InvokeShutdown() InvokeShutdown()

同步启动 Dispatcher 的关闭过程。Initiates the shutdown process of the Dispatcher synchronously.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
PushFrame(DispatcherFrame) PushFrame(DispatcherFrame) PushFrame(DispatcherFrame) PushFrame(DispatcherFrame)

进入执行循环。Enters an execute loop.

Run() Run() Run() Run()

将主执行帧推送到 Dispatcher 的事件队列中。Pushes the main execution frame on the event queue of the Dispatcher.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
ValidatePriority(DispatcherPriority, String) ValidatePriority(DispatcherPriority, String) ValidatePriority(DispatcherPriority, String) ValidatePriority(DispatcherPriority, String)

确定指定的 DispatcherPriority 是否为有效的优先级。Determines whether the specified DispatcherPriority is a valid priority.

VerifyAccess() VerifyAccess() VerifyAccess() VerifyAccess()

确定调用线程是否可以访问此 DispatcherDetermines whether the calling thread has access to this Dispatcher.

Yield() Yield() Yield() Yield()

创建异步产生控制权交还给当前计划程序并为该计划程序提供机会处理其他事件的可等待对象。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) Yield(DispatcherPriority) Yield(DispatcherPriority) Yield(DispatcherPriority)

创建异步产生控制权交还给当前计划程序并为该计划程序提供机会处理其他事件的可等待对象。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 ShutdownFinished ShutdownFinished ShutdownFinished

Dispatcher 完成关闭时发生。Occurs when the Dispatcher finishes shutting down.

ShutdownStarted ShutdownStarted ShutdownStarted ShutdownStarted

Dispatcher 开始关闭时发生。Occurs when the Dispatcher begins to shut down.

UnhandledException UnhandledException UnhandledException UnhandledException

在通过 InvokeBeginInvoke 执行委托的过程中,如果引发线程异常且未能捕获该异常,则发生此事件。Occurs when a thread exception is thrown and uncaught during execution of a delegate by way of Invoke or BeginInvoke.

UnhandledExceptionFilter UnhandledExceptionFilter UnhandledExceptionFilter 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) BeginInvoke(Dispatcher, Action) BeginInvoke(Dispatcher, Action) 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) BeginInvoke(Dispatcher, Action, DispatcherPriority) BeginInvoke(Dispatcher, Action, DispatcherPriority) 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) Invoke(Dispatcher, Action) Invoke(Dispatcher, Action) 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) Invoke(Dispatcher, Action, TimeSpan) Invoke(Dispatcher, Action, TimeSpan) 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) Invoke(Dispatcher, Action, TimeSpan, DispatcherPriority) Invoke(Dispatcher, Action, TimeSpan, DispatcherPriority) 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) Invoke(Dispatcher, Action, DispatcherPriority) Invoke(Dispatcher, Action, DispatcherPriority) Invoke(Dispatcher, Action, DispatcherPriority)

用指定的优先级在依据其创建指定 Dispatcher 的线程上同步执行指定的委托。Executes the specified delegate synchronously with the specified priority on the thread that the specified Dispatcher was created on.

适用于

另请参阅