Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Dispatcher.Invoke Method

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

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  NameDescription
Public method Invoke(DispatcherPriority, Delegate) Executes the specified delegate synchronously at the specified priority on the thread on which the Dispatcher is associated with.
Public method Invoke(Delegate, Object[]) Executes the specified delegate with the specified arguments synchronously on the thread the Dispatcher is associated with.
Public method 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.
Public method 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.
Public method 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.
Public method 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.
Public method 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.
Public method 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.
Public method 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.
Public method 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.
Top

In WPF, only the thread that created a DispatcherObject may access that object. For example, a background thread that is spun off from the main UI thread cannot update the contents of a Button that was created 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 event queue of the Dispatcher at the specified DispatcherPriority.

Invoke is a synchronous operation; therefore, control will not return to the calling object until after the callback returns.

The following example places a delegate onto a Dispatcher at Normal using Invoke. For the full source of the example, see Disable Command Source via Dispatcher Timer Sample.

// Places the delegate onto the UI Thread's Dispatcher 
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    // Place delegate on the Dispatcher. 
    this.Dispatcher.Invoke(DispatcherPriority.Normal,
        new TimerDispatcherDelegate(TimerWorkItem));
}

Community Additions

Show:
© 2017 Microsoft