Skip to main content

Microsoft Silverlight

Answered Question Is Dispatcher Required in BackgroundWorker?RSS Feed

(0)

HaywireGuy
HaywireGuy

Member

Member

12 points

15 Posts

Is Dispatcher Required in BackgroundWorker?

Hi people,

I'm done reading the article "Build More Responsive Apps With The Dispatcher". That's where it says:

By using BackgroundWorker, the Dispatcher is being employed automatically to invoke cross-thread method calls.

A BackgroundWorker notifies main UI-thread when it finishes operation through its BackgroundWorker::RunWorkerCompleted event. The question is, can this event be fired in UI-thread when UI-thread is still running other functions (e.g. UpdateFrame)? Or  RunWorkerCompleted event is guaranteed to be dispatched correctly when UI-thread is not doing anything?

1    class MyPage : Page
2    {
3        // Called from within CompositionTarget.Rendering event.
4        void UpdateFrame()
5        {
6            DoTaskOne();
7            DoTaskTwo();
8            // Can  BackgroundWorker::RunWorkerCompleted be raised somewhere here?
9            DoTaskThree();
10       }
11   }
12   

 

Thanks in advance,
Ben.

------------------------------------------------

Stackana - Practice Japanese Hiragana/Katakana through Silverlight Game.

ksleung
ksleung

Contributor

Contributor

5396 points

1,028 Posts

Answered Question

Re: Is Dispatcher Required in BackgroundWorker?

The answer based on my understanding...

> can this event be fired in UI-thread when UI-thread is still running other functions (e.g. UpdateFrame)?

To be precise, the event will be "queued up" right away but the event handler will not be "fired" right away.

Or RunWorkerCompleted event is guaranteed to be dispatched correctly when UI-thread is not doing anything?

Yes.

when the thread is completed, RunWorkerCompleted event is *queued up* (asynchronously) to be executed (synchronously) in the UI thread, so it won't be an unwelcomed interrupt to currently executed functions in the UI thread.

The downside, of course, is that there is only one UI thread.

NigelLeeming
NigelLee...

Member

Member

32 points

22 Posts

Re: Is Dispatcher Required in BackgroundWorker?

 Ben

You may be interested in the example I have posted in http://silverlight.net/forums/t/111299.aspx. As far as I can tell, the BackgroundWorker does need to directly invoke the dispatcher when creating UI elements.

Nigel 

HaywireGuy
HaywireGuy

Member

Member

12 points

15 Posts

Re: Is Dispatcher Required in BackgroundWorker?

Thanks guys, that helps! This property of BackgroundWorker makes it very useful.

Nigel, I am taking a while longer to understand the codes in your example. I will have a look at it soon, thanks anyway Smile

------------------------------------------------

Stackana - Practice Japanese Hiragana/Katakana through Silverlight Game.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities