I'm trying to make the UI of a page in a WP7 application more responsive by putting the data loading portion into a background thread rather than running in the foreground when the page loads.
The thread code essentially works through some data and adds items to an observable collection; in order to avoid exception issues, I execute something like:
Deployment.Current.Dispatcher.BeginInvoke(() => { _events.Add(_newItem); });
so that the addition of the item to the collection is done in the UI thread.
The problem I'm now hitting is that a subsequent part of the code needs to perform a foreach on the collection in order to figure out where to insert a new item rather than just add it. Unfortunately, what I'm finding is that the UI thread can sometimes perform its Add while I'm in the foreach loop, instantly breaking the foreach.
From the reading I've done, it looks like one approach would be to call EndInvoke() in order to block the background thread until the UI piece is done. Unfortunately, it looks like thw Wp7/Silverlight implementation doesn't support EndInvoke.
Any suggestions on how I can check that the Add has been completed before I start the foreach?
Thanks.
Philip