Modifier

Partager via


Dispatcher.BeginInvoke Method

Definition

Executes a delegate asynchronously on the thread the Dispatcher is associated with.

Overloads

BeginInvoke(Delegate, Object[])

Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on.

BeginInvoke(DispatcherPriority, Delegate)

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

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

Executes the specified delegate asynchronously at the specified priority and with the specified array of arguments on the thread the Dispatcher is associated with.

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.

BeginInvoke is asynchronous; therefore, control returns immediately to the calling object after it is called.

BeginInvoke returns a DispatcherOperation object that can be used to interact with the delegate when the delegate is in the event queue.

The DispatcherOperation object returned by BeginInvoke can be used in several ways to interact with the specified delegate, such as:

  • Changing the DispatcherPriority of the delegate as it is pending execution in the event queue.

  • Removing the delegate from the event queue.

  • Waiting for the delegate to return.

  • Obtaining the value that the delegate returns after it is executed.

If multiple BeginInvoke calls are made at the same DispatcherPriority, they will be executed in the order the calls were made.

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

BeginInvoke(Delegate, Object[])

Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on.

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(Delegate ^ method, ... cli::array <System::Object ^> ^ args);
public System.Windows.Threading.DispatcherOperation BeginInvoke (Delegate method, params object[] args);
member this.BeginInvoke : Delegate * obj[] -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (method As Delegate, ParamArray args As Object()) As DispatcherOperation

Parameters

method
Delegate

The 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

An object, which is returned immediately after BeginInvoke is called, that can be used to interact with the delegate as it is pending execution in the event queue.

Remarks

The DispatcherOperation object returned by BeginInvoke can be used in several ways to interact with the specified delegate, such as:

  • Changing the DispatcherPriority of the delegate as it is pending execution in the event queue.

  • Removing the delegate from the event queue.

  • Waiting for the delegate to return.

  • Obtaining the value that the delegate returns after it is executed.

BeginInvoke is asynchronous; therefore, control returns immediately to the calling object after it is called.

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.

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

Applies to

BeginInvoke(DispatcherPriority, Delegate)

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

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(System::Windows::Threading::DispatcherPriority priority, Delegate ^ method);
[System.ComponentModel.Browsable(false)]
public System.Windows.Threading.DispatcherOperation BeginInvoke (System.Windows.Threading.DispatcherPriority priority, Delegate method);
[<System.ComponentModel.Browsable(false)>]
member this.BeginInvoke : System.Windows.Threading.DispatcherPriority * Delegate -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (priority As DispatcherPriority, method As Delegate) As DispatcherOperation

Parameters

priority
DispatcherPriority

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

method
Delegate

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

Returns

An object, which is returned immediately after BeginInvoke is called, that can be used to interact with the delegate as it is pending execution in the event queue.

Attributes

Exceptions

method is null.

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. Because every DispatcherObject has a property that returns the Dispatcher it is associated with, the desired Dispatcher is obtained by querying the DispatcherObject, in this case a Button named startStopButton. The 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

If multiple BeginInvoke calls are made at the same DispatcherPriority, they will be executed in the order the calls were made.

BeginInvoke returns a DispatcherOperation object that can be used to interact with the delegate when the delegate is in the event queue.

The DispatcherOperation object returned by BeginInvoke can be used in several ways to interact with the specified delegate, such as:

  • Changing the DispatcherPriority of the delegate as it is pending execution in the event queue.

  • Removing the delegate from the event queue.

  • Waiting for the delegate to return.

  • Obtaining the value that the delegate returns after it is executed.

BeginInvoke is asynchronous; therefore, control returns immediately to the calling object after it is called.

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.

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

See also

Applies to

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.

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(Delegate ^ method, System::Windows::Threading::DispatcherPriority priority, ... cli::array <System::Object ^> ^ args);
public System.Windows.Threading.DispatcherOperation BeginInvoke (Delegate method, System.Windows.Threading.DispatcherPriority priority, params object[] args);
member this.BeginInvoke : Delegate * System.Windows.Threading.DispatcherPriority * obj[] -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (method As Delegate, priority As DispatcherPriority, ParamArray args As Object()) As DispatcherOperation

Parameters

method
Delegate

The 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, with which the specified method is invoked.

args
Object[]

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

Returns

An object, which is returned immediately after BeginInvoke is called, that can be used to interact with the delegate as it is pending execution in the event queue.

Remarks

The DispatcherOperation object returned by BeginInvoke can be used in several ways to interact with the specified delegate, such as:

  • Changing the DispatcherPriority of the delegate as it is pending execution in the event queue.

  • Removing the delegate from the event queue.

  • Waiting for the delegate to return.

  • Obtaining the value that the delegate returns after it is executed.

BeginInvoke is asynchronous; therefore, control returns immediately to the calling object after it is called.

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.

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

Applies to

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.

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(System::Windows::Threading::DispatcherPriority priority, Delegate ^ method, System::Object ^ arg);
[System.ComponentModel.Browsable(false)]
public System.Windows.Threading.DispatcherOperation BeginInvoke (System.Windows.Threading.DispatcherPriority priority, Delegate method, object arg);
[<System.ComponentModel.Browsable(false)>]
member this.BeginInvoke : System.Windows.Threading.DispatcherPriority * Delegate * obj -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (priority As DispatcherPriority, method As Delegate, arg As Object) As DispatcherOperation

Parameters

priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, with which 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

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

Returns

An object, which is returned immediately after BeginInvoke is called, that can be used to interact with the delegate as it is pending execution in the event queue.

Attributes

Exceptions

method is null.

Examples

The following example shows how to place an operation onto a Dispatcher.

First, a delegate is created that accepts one argument, in this case a string.

private delegate void OneArgDelegate(String arg);
Private Delegate Sub OneArgDelegate(ByVal arg As String)

Next, BeginInvoke(DispatcherPriority, Delegate, Object) is called. Because every DispatcherObject has a property that returns the Dispatcher it is associated with, the desired Dispatcher is obtained by querying the DispatcherObject, in this case a Grid named tomorrowsWeather. The call to BeginInvoke(DispatcherPriority, Delegate, Object) takes three parameters: the priority, which is set to DispatcherPriority.Normal; the callback, which is passed in through an instance of the delegate OneArgDelegate; and a string named weather, which is the argument for the callback.

// Schedule the update function in the UI thread.
tomorrowsWeather.Dispatcher.BeginInvoke(
    System.Windows.Threading.DispatcherPriority.Normal,
    new OneArgDelegate(UpdateUserInterface), 
    weather);
' Schedule the update function in the UI thread.
tomorrowsWeather.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, New OneArgDelegate(AddressOf UpdateUserInterface), weather)

Remarks

arg can be null if no arguments are needed.

BeginInvoke returns a DispatcherOperation object that can be used to interact with the delegate when the delegate is in the event queue.

The DispatcherOperation object returned by BeginInvoke can be used in several ways to interact with the specified delegate, such as:

  • Changing the DispatcherPriority of the delegate as it is pending execution in the event queue.

  • Removing the delegate from the event queue.

  • Waiting for the delegate to return.

  • Obtaining the value that the delegate returns after it is executed.

BeginInvoke is asynchronous; therefore, control returns immediately to the calling object after it is called.

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.

If multiple BeginInvoke calls are made at the same DispatcherPriority, they will be executed in the order the calls were made.

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

See also

Applies to

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.

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(System::Windows::Threading::DispatcherPriority priority, Delegate ^ method, System::Object ^ arg, ... cli::array <System::Object ^> ^ args);
[System.ComponentModel.Browsable(false)]
public System.Windows.Threading.DispatcherOperation BeginInvoke (System.Windows.Threading.DispatcherPriority priority, Delegate method, object arg, params object[] args);
[<System.ComponentModel.Browsable(false)>]
member this.BeginInvoke : System.Windows.Threading.DispatcherPriority * Delegate * obj * obj[] -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (priority As DispatcherPriority, method As Delegate, arg As Object, ParamArray args As Object()) As DispatcherOperation

Parameters

priority
DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, with which 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

The 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

An object, which is returned immediately after BeginInvoke is called, that can be used to interact with the delegate as it is pending execution in the Dispatcher queue.

Attributes

Exceptions

method is null.

Remarks

The arg parameter can be null if no arguments are needed.

BeginInvoke returns a DispatcherOperation object that can be used to interact with the delegate when the delegate is in the event queue.

The DispatcherOperation object returned by BeginInvoke can be used in several ways to interact with the specified delegate, such as:

  • Changing the DispatcherPriority of the delegate as it is pending execution in the event queue.

  • Removing the delegate from the event queue.

  • Waiting for the delegate to return.

  • Obtaining the value that the delegate returns after it is executed.

BeginInvoke is asynchronous; therefore, control returns immediately to the calling object after it is called.

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.

If multiple BeginInvoke calls are made at the same DispatcherPriority, they will be executed in the order the calls were made.

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

See also

Applies to