Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Dispatcher Class
Invoke Method

  Switch on low bandwidth view
Members FilterMembers Filter
Frameworks FilterFrameworks Filter
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Dispatcher..::.Invoke Method

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

  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

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.

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Parameter Ordering      antonpious   |   Edit   |   Show History

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 What's this?: Add a tag
Flag as ContentBug
Invoke fails silently when Dispatcher has not started up yet      Chris Nahr   |   Edit   |   Show History
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 What's this?: Add a tag
Flag as ContentBug
Parameter Ordering - SP1      Alex Vallat   |   Edit   |   Show History

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 What's this?: sp1 (x) Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker