Jepzen
Jepzen

Reputation: 3162

Backgroundworker calling Dispatcher.Invoke

I am working on this code written by some others. It contains a background worker which from its DoWork calls a Dispatcher.Invoke.

My problem is that i think my BackgroundWorkerCompleted will happen before my Dispatcher.Invoke is done. Is that correct?

Will Dispatcher.Invoke start a new thread or start on the main one?

Is it a bad idea to mix Dispatcher with Backgroundworker ? The samples i find seems to be using only one of them.

Upvotes: 0

Views: 1396

Answers (1)

Habib
Habib

Reputation: 223217

My problem is that i think my BackgroundWorkerCompleted will happen before my Dispatcher.Invoke is done. Is that correct?

No, thats not correct.

Will Dispatcher.Invoke start a new thread or start on the main one?

No, it will execute the code on UI thread. Thus it will prevent you from getting cross threaded exception. Usually used to modify GUI contents.

See Dispatcher.Invoke

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.

Upvotes: 1

Related Questions