views:

73

answers:

1

Hi,

I am using the Background worker thread in a Silverlight 4 application. In the ProgresssChanged event handler, I can make calls to the Silverlight UI, but how does this work ? Isn't the background worker thread that fires the ProgressChanged event on a different thread from the Silverlight UI thread ? If so, I thought updating the UI thread from another thread causes exceptions.

Scott

+2  A: 

You're right that updating the UI Thread from another thread is a no-no. BackgroundWorker has special smarts to make sure, even though work is being done on a background thread, the ProgressChanged and RunWorkerCompleted events are raised on the UI Thread.

I believe it makes use of SynchronizationContext to do this.

Samuel Jack
Hmm. Just read SynchronizationContext and I must confess, it didn't shed a lot of light on it for me. I am guessing the Background worker thread inherits from this ? Thanks for your help.
Scott Davies
+1 It does use the `SynchronizationContext` to ensure that the Progress and Completed events occur on the __same__ thread that called the `RunWorkerAsync`. As result its only safe to update the UI from these events if the `RunWorkerAsync` was actually called from the UI Thread. BackgroundWorker doesn't specifically seek out the UI Thread.
AnthonyWJones