Hi,
I'm in trouble when using the background worker to create my object model.
As I understand why, I'm unable to find a workaround.
Here is the pseudo logic :
Call Webservice async
When received, open a background worker, and load data into controls in the background
in the Load method, search for an existing object and if not found, create a new one.
All objects created inherits from Control (with a transparent abstract layer).
While I create controls on the main thread, the function work fine :
public static T Find<T>(ObservableCollection<T> collection, int objectId)
where T : FormaliteBaseControl, new()
{
foreach (T item in collection)
{
if (item.ObjectId == objectId)
return item;
}
return new T();
}
Of course, when called from the background thread, a cross thread exception occurs at "new T()" In Silverlight, there is no way to call Dispatcher.Invoke synchronously.
And while I'm creating around 450 objects in this way, I would like to keep the object model creation in the Background without decreasing performance with Thread.Sleep or other "great" methods.
Thanks for your answers.