Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Is Dispatcher Required in BackgroundWorker?
3 replies. Latest Post by HaywireGuy on July 19, 2009.
(0)
HaywireGuy
Member
12 points
15 Posts
07-19-2009 2:53 AM |
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.
ksleung
Contributor
5396 points
1,028 Posts
07-19-2009 3:01 AM |
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.
NigelLee...
32 points
22 Posts
07-19-2009 3:34 AM |
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
07-19-2009 10:10 AM |
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