Dispatcher Dispatcher Dispatcher Dispatcher Class

Definition

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
Inheritance
DispatcherDispatcherDispatcherDispatcher

Examples

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()

Next, BeginInvoke(DispatcherPriority, Delegate) is called. 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))

Remarks

The Dispatcher maintains a prioritized queue of work items for a specific thread.

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.

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. A Dispatcher is also created when you create a DispatcherObject. If you create a Dispatcher on a background thread, be sure to shut down the dispatcher before exiting the thread.

If a Dispatcher is shut down, it cannot be restarted.

In WPF, a DispatcherObject can only be accessed by the Dispatcher it is associated with. For example, a background thread cannot update the contents of a Button that is associated with the Dispatcher on the UI thread. In 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 UI thread. This is accomplished by using either Invoke or BeginInvoke. Invoke is synchronous and BeginInvoke is asynchronous. The operation is added to the queue of the Dispatcher at the specified DispatcherPriority.

If BeginInvoke is called on a Dispatcher that has shut down, the status property of the returned DispatcherOperation is set to Aborted.

All of the methods on Dispatcher, with the exception of DisableProcessing, are free-threaded.

Objects that derive from DispatcherObject have thread affinity.

Objects that derive from Freezable are free-threaded when they are frozen. For more information, see Freezable Objects Overview.

Properties

CurrentDispatcher CurrentDispatcher CurrentDispatcher CurrentDispatcher

Gets 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

Determines whether the Dispatcher has finished shutting down.

HasShutdownStarted HasShutdownStarted HasShutdownStarted HasShutdownStarted

Determines whether the Dispatcher is shutting down.

Hooks Hooks Hooks Hooks

Gets the collection of hooks that provide additional event information about the Dispatcher.

Thread Thread Thread Thread

Gets the thread this Dispatcher is associated with.

Methods

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

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[])

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)

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)

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[])

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)

Initiates shutdown of the Dispatcher asynchronously.

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

Determines whether the calling thread is the thread associated with this Dispatcher.

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

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)

Gets the Dispatcher for the specified thread.

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

Serves as the default hash function.

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

Gets the Type of the current instance.

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

Executes the specified Action synchronously on the thread the Dispatcher is associated with.

Invoke(Action, DispatcherPriority) Invoke(Action, DispatcherPriority) Invoke(Action, DispatcherPriority) Invoke(Action, DispatcherPriority)

Executes 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)

Executes 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)

Executes 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[])

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[])

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[])

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[])

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)

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)

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[])

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)

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)

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[])

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>)

Executes 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)

Executes 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)

Executes 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)

Executes 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)

Executes the specified Action asynchronously on the thread the Dispatcher is associated with.

InvokeAsync(Action, DispatcherPriority) InvokeAsync(Action, DispatcherPriority) InvokeAsync(Action, DispatcherPriority) InvokeAsync(Action, DispatcherPriority)

Executes 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)

Executes 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>)

Executes 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)

Executes 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)

Executes the specified Func<TResult> asynchronously at the specified priority on the thread the Dispatcher is associated with.

InvokeShutdown() InvokeShutdown() InvokeShutdown() InvokeShutdown()

Initiates the shutdown process of the Dispatcher synchronously.

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

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()

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)

Determines whether the specified DispatcherPriority is a valid priority.

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

Determines 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.

Events

ShutdownFinished ShutdownFinished ShutdownFinished ShutdownFinished

Occurs when the Dispatcher finishes shutting down.

ShutdownStarted ShutdownStarted ShutdownStarted ShutdownStarted

Occurs when the Dispatcher begins to shut down.

UnhandledException UnhandledException UnhandledException UnhandledException

Occurs when a thread exception is thrown and uncaught during execution of a delegate by way of Invoke or BeginInvoke.

UnhandledExceptionFilter UnhandledExceptionFilter UnhandledExceptionFilter UnhandledExceptionFilter

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.

Extension Methods

BeginInvoke(Dispatcher, Action) BeginInvoke(Dispatcher, Action) BeginInvoke(Dispatcher, Action) BeginInvoke(Dispatcher, Action)

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)

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)

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)

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)

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)

Executes the specified delegate synchronously with the specified priority on the thread that the specified Dispatcher was created on.

Applies to

See also