Dispatcher.​Invoke Method

Definition

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

Overloads

Invoke(​Dispatcher​Priority, ​Time​Span, ​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(​Dispatcher​Priority, ​Time​Span, ​Delegate, ​Object)

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

Invoke(​Dispatcher​Priority, ​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(​Action, ​Dispatcher​Priority, ​Cancellation​Token, ​Time​Span)

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

Invoke(​Dispatcher​Priority, ​Time​Span, ​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(​Dispatcher​Priority, ​Delegate, ​Object)

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

Invoke(​Delegate, ​Time​Span, ​Dispatcher​Priority, ​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, ​Time​Span, ​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(​Action, ​Dispatcher​Priority, ​Cancellation​Token)

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

Invoke(​Dispatcher​Priority, ​Delegate)

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

Invoke(​Delegate, ​Object[])

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

Invoke(​Action, ​Dispatcher​Priority)

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

Invoke(​Action)

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

Invoke(​Delegate, ​Dispatcher​Priority, ​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>)

Executes the specified Func<TResult> synchronously on the thread the Dispatcher is associated with.

Invoke<​TResult>(​Func<​TResult>, ​Dispatcher​Priority)

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

Invoke<​TResult>(​Func<​TResult>, ​Dispatcher​Priority, ​Cancellation​Token)

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

Invoke<​TResult>(​Func<​TResult>, ​Dispatcher​Priority, ​Cancellation​Token, ​Time​Span)

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

Examples

The following example places a delegate onto a Dispatcher at Normal using Invoke.

// 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));
}
' Places the delegate onto the UI Thread's Dispatcher
Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs)
	' Place delegate on the Dispatcher.
	Me.Dispatcher.Invoke(DispatcherPriority.Normal, New TimerDispatcherDelegate(AddressOf TimerWorkItem))
End Sub

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.

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.

[System.ComponentModel.Browsable(false)]
public object Invoke (System.Windows.Threading.DispatcherPriority priority, TimeSpan timeout, Delegate method, object arg, object[] args);
Parameters
priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, the specified method is invoked.

timeout
TimeSpan

The maximum time to wait for the operation to finish.

method
Delegate

A delegate to a method that takes multiple arguments, which is pushed onto the Dispatcher event queue.

arg
Object

An object to pass as an argument to the specified method.

args
Object[]

An array of objects to pass as arguments to the specified method.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

Attributes
Exceptions

priority is equal to Inactive.

method is null.

Remarks

Arg can be null if an argument is not needed.

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.

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.

[System.ComponentModel.Browsable(false)]
public object Invoke (System.Windows.Threading.DispatcherPriority priority, TimeSpan timeout, Delegate method, object arg);
Parameters
priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, the specified method is invoked.

timeout
TimeSpan

The maximum time to wait for the operation to finish.

method
Delegate

A delegate to a method that takes multiple arguments, which is pushed onto the Dispatcher event queue.

arg
Object

An object to pass as an argument to the given method. This can be null if no arguments are needed.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

Attributes
Exceptions

priority is equal to Inactive.

priority is not a valid priority.

method is null.

Remarks

Arg can be null if an argument is not needed

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.

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.

[System.ComponentModel.Browsable(false)]
public object Invoke (System.Windows.Threading.DispatcherPriority priority, Delegate method, object arg, object[] args);
Parameters
priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, the specified method is invoked.

method
Delegate

A delegate to a method that takes multiple arguments, which is pushed onto the Dispatcher event queue.

arg
Object

An object to pass as an argument to the given method.

args
Object[]

An array of objects to pass as arguments to the given method.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

Attributes
Exceptions

priority is equal to Inactive.

priority is not a valid priority.

method is null.

Remarks

Arg can be null if an argument is not needed

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.

Invoke(Action, DispatcherPriority, CancellationToken, TimeSpan)

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

public void Invoke (Action callback, System.Windows.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken, TimeSpan timeout);
Parameters
callback
Action

A delegate to invoke through the dispatcher.

priority
DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

cancellationToken
CancellationToken

An object that indicates whether to cancel the action.

timeout
TimeSpan

The minimum amount of time to wait for the operation to start.

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.

[System.ComponentModel.Browsable(false)]
public object Invoke (System.Windows.Threading.DispatcherPriority priority, TimeSpan timeout, Delegate method);
Parameters
priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, the specified method is invoked.

timeout
TimeSpan

The maximum time to wait for the operation to finish.

method
Delegate

The delegate to a method that takes no arguments, which is pushed onto the Dispatcher event queue.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

Attributes

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.

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.

[System.ComponentModel.Browsable(false)]
public object Invoke (System.Windows.Threading.DispatcherPriority priority, Delegate method, object arg);
Parameters
priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, the specified method is invoked.

method
Delegate

A delegate to a method that takes one argument, which is pushed onto the Dispatcher event queue.

arg
Object

An object to pass as an argument to the given method.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

Attributes
Exceptions

priority is equal to Inactive.

priority is not a valid priority.

method is null.

Remarks

Arg can be null if an argument is not needed

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.

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 object Invoke (Delegate method, TimeSpan timeout, System.Windows.Threading.DispatcherPriority priority, object[] args);
Parameters
method
Delegate

A delegate to a method that takes parameters specified in args, which is pushed onto the Dispatcher event queue.

timeout
TimeSpan

The maximum amount of time to wait for the operation to complete.

priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, the specified method is invoked.

args
Object[]

An array of objects to pass as arguments to the given method. Can be null.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

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.

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 object Invoke (Delegate method, TimeSpan timeout, object[] args);
Parameters
method
Delegate

A delegate to a method that takes parameters specified in args, which is pushed onto the Dispatcher event queue.

timeout
TimeSpan

The maximum amount of time to wait for the operation to complete.

args
Object[]

An array of objects to pass as arguments to the given method. Can be null.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

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.

Invoke(Action, DispatcherPriority, CancellationToken)

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

public void Invoke (Action callback, System.Windows.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken);
Parameters
callback
Action

A delegate to invoke through the dispatcher.

priority
DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

cancellationToken
CancellationToken

An object that indicates whether to cancel the action.

Invoke(DispatcherPriority, Delegate)

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

[System.ComponentModel.Browsable(false)]
public object Invoke (System.Windows.Threading.DispatcherPriority priority, Delegate method);
Parameters
priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, the specified method is invoked.

method
Delegate

A delegate to a method that takes no arguments, which is pushed onto the Dispatcher event queue.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

Attributes
Exceptions

priority is equal to Inactive.

priority is not a valid priority.

method is null.

Examples

The following example places a delegate onto a Dispatcher at Normal using Invoke.

// 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));
}
' Places the delegate onto the UI Thread's Dispatcher
Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs)
	' Place delegate on the Dispatcher.
	Me.Dispatcher.Invoke(DispatcherPriority.Normal, New TimerDispatcherDelegate(AddressOf TimerWorkItem))
End Sub

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.

Invoke(Delegate, Object[])

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

public object Invoke (Delegate method, object[] args);
Parameters
method
Delegate

A delegate to a method that takes parameters specified in args, which is pushed onto the Dispatcher event queue.

args
Object[]

An array of objects to pass as arguments to the given method. Can be null.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

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.

Invoke(Action, DispatcherPriority)

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

public void Invoke (Action callback, System.Windows.Threading.DispatcherPriority priority);
Parameters
callback
Action

A delegate to invoke through the dispatcher.

priority
DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

Invoke(Action)

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

public void Invoke (Action callback);
Parameters
callback
Action

A delegate to invoke through the dispatcher.

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 object Invoke (Delegate method, System.Windows.Threading.DispatcherPriority priority, object[] args);
Parameters
method
Delegate

A delegate to a method that takes parameters specified in args, which is pushed onto the Dispatcher event queue.

priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, the specified method is invoked.

args
Object[]

An array of objects to pass as arguments to the given method. Can be null.

Returns

The return value from the delegate being invoked or null if the delegate has no return value.

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.

Invoke<TResult>(Func<TResult>)

Executes the specified Func<TResult> synchronously on the thread the Dispatcher is associated with.

public TResult Invoke<TResult> (Func<TResult> callback);
Type Parameters
TResult

The return value type of the specified delegate.

Parameters
callback
Func<TResult>

A delegate to invoke through the dispatcher.

Returns
TResult

Remarks

The value returned by callback.

Invoke<TResult>(Func<TResult>, DispatcherPriority)

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

public TResult Invoke<TResult> (Func<TResult> callback, System.Windows.Threading.DispatcherPriority priority);
Type Parameters
TResult

The return value type of the specified delegate.

Parameters
callback
Func<TResult>

A delegate to invoke through the dispatcher.

priority
DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

Returns
TResult

Remarks

The value returned by callback.

Invoke<TResult>(Func<TResult>, DispatcherPriority, CancellationToken)

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

public TResult Invoke<TResult> (Func<TResult> callback, System.Windows.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken);
Type Parameters
TResult

The return value type of the specified delegate.

Parameters
callback
Func<TResult>

A delegate to invoke through the dispatcher.

priority
DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

cancellationToken
CancellationToken

An object that indicates whether to cancel the operation.

Returns
TResult

Remarks

The value returned by callback.

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.

public TResult Invoke<TResult> (Func<TResult> callback, System.Windows.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken, TimeSpan timeout);
Type Parameters
TResult

The return value type of the specified delegate.

Parameters
callback
Func<TResult>

A delegate to invoke through the dispatcher.

priority
DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

cancellationToken
CancellationToken

An object that indicates whether to cancel the operation.

timeout
TimeSpan

The minimum amount of time to wait for the operation to start.

Returns
TResult

Remarks

The value returned by callback.