Silverlight wont update UI from background thread

by Bobbi Perreault 25. May 2008 09:16
Share on Facebook

When I did this:  Used a web service to retrieve an XML serialized list of items from my database, and used the deserialized list to fill a member variable that was a list.

This happened:  Within the method which was called from the asyncronous retrieve - the member variable that was a list was filled with the appropriate number of class instances.

As soon as that same list was accessed from an event handler, the count would again show 0 items as if there never were any items from the retrieve.

Here's what I found out about it, and how I fixed it: 
In Silverlight applications, you aren't allowed to modify the UI thread from a background thread.  Of course, the last event of an asyncronous call to a web service isn't on the UI thread.  This line of code insures that the method which fills  my member vars is executed on the UI thread:

Dispatcher.BeginInvoke(() => setMappedItems());


That is, setMappedItems() is the method which fills the member list.

Walla, no more missing items in my list.   I found this useful information in this blog here:  http://adoguy.com/2008/05/06/Executing_Code_on_the_UI_Thread_in_Silverlight_2.aspx

Comments are closed

 

RSS Feed FriendFeed