views:

28

answers:

2

I have a deserialization method (XML) that functions well until it gets to X number of objects. Below X, the time is takes to deserialize is acceptable and the UI being blocked is not a problem. However, if the number of objects is greater than X (relatively unlikely, but possible), then the load time is too long. Is it possible to jump from the UI thread to a background thread once deserialization (or any other method) has started so that I can unlock the UI thread if the deserialization is taking too long? thanks

A: 

No, once you have requested deserialization from a BCL facility, you cannot switch the thread type. Perhaps if you wrote your own custom deserializer you could do this, but it would be far more effective to simply have a seperate thread (e.g. threadpool) do this task in all cases (on account of the big cases), rather than after you cross boundary X.

Brent Arias
+2  A: 

Just do the deserialization in a background thread in all cases. It won't be an issue if the number of objects is below X, and it won't block the UI if it is greater than X.

You can do it in a background worker, and use the result in the RunWorkerCompleted event (which is raised on the UI thread)

Thomas Levesque
thanks. I guess this will be the best way. I didn't initially want to do all of it on the bg because the user may try to access the objects before deserialization was completed. However, with that event notification, I can check if it's done.
SSL