views:

29

answers:

1

Hi,

I am using a BackgroundWorker thread to do some work outside of the GUI thread in Silverlight 4. I would like to update widgets in the GUI context from the background thread, but have read warnings about doing so from Microsoft documentation. I understand that communicating from one thread to another throws an exception as only the GUI thread can update widgets, but isn't BeginInvoke provided for this:

// BackgroundWorker thread code...

Widget.Dispatcher.BeginInvoke(() => Status.Text = "Hello");

However, I am getting some unpredictable results in my worker code.

What is the proper way to cross communicate with the GUI thread ? Is it by passing a results collection out of the RunWorkerCompleted method ?

Thanks,

Scott

+1  A: 

You don't call the GUI code directly.

You must fire and event to which the UI thread subscribes. It then does the marshalling necessary to update the UI safely.

There are plenty of questions (and answers) on this. See some of the "Related" questions in the right hand side bar.

ChrisF
Ah, thanks Chris. Yes, the resources on the side have been helpful.
Scott Davies