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

示例

下面的示例演示如何将操作置于 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 时,即使 Dispatcher 关闭,它也会成为可以与线程关联的唯一 DispatcherWhen 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 与该线程不关联,则将创建一个 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. 当你创建 DispatcherObject时,还会创建一个 DispatcherA 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.

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

如果对已关闭的 Dispatcher 调用 BeginInvoke,则返回 DispatcherOperation 的 "状态" 属性设置为 "Aborted"。If BeginInvoke is called on a Dispatcher that has shut down, the status property of the returned DispatcherOperation is set to Aborted.

DisableProcessing以外,Dispatcher上的所有方法均可自由线程。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,并在该线程没有关联的调度程序时创建一个新的 DispatcherGets 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)

按指定的优先级在与 Dispatcher 关联的线程上同步执行指定的委托。Executes the specified delegate synchronously at the specified priority on the thread on which 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()

返回一个表示当前对象的 string。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()

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

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

适用于

另请参阅