.NET Framework Class Library
Dispatcher..::.Invoke Method

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

Overload List

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

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.

Examples

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.

C#
// 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));
}
See Also

Reference

Tags :


Community Content

antonpious
Parameter Ordering

The IDE Shows only 4 overrloads and all of them start with delegate method as the first parameter, the documentation requires reordering of parameters

Tags :

Chris Nahr
Invoke fails silently when Dispatcher has not started up yet
Invoke fails silently when called on a Dispatcher that has not finished starting up yet. The call does not block and the operation is not executed in this case, but simply discarded. Unlike BeginInvoke, which returns a Status of Aborted in this case, there seems to be no way to discover this situation for Invoke calls.

This can happen in race conditions when an Invoke call is made very soon after a valid Dispatcher object has been obtained, but before its Dispatcher.Run loop is up and running on the target thread. Unfortunately, Dispatcher provides no flag to check for this condition, so if your code is affected by this race condition, you cannot (reliably) use Invoke. Instead, use a BeginInvoke call and wrap it in a do...while loop that repeats while the returned Status is Aborted.
Tags :

Alex Vallat
Parameter Ordering - SP1

Overloads starting with the Delegate parameter fail under .Net 3.5 with a MissingMethodException. .Net 3.5 SP1 is required for them to work.

Overloads starting with a DispatcherPriority parameter work under both, but are hidden from Intellisense.

Tags : sp1

Page view tracker