Edit: My question is not answered by related threads because I want to avoid adding boilerplate every time I need to interact with an Observable Collection.
I do not need the application to be responsive while my loading operation plays out. What I need, really, is for one window (the loading popup) to animate and update even when the main window is frozen. I would have thought this was simple.
I have a 3D scene containing a hierarchy of 3D objects. Some objects are serialised as Json, and I have a button on these objects called 'Export And Reimport', which serialises and reloads that object.
This process often involves rebuilding the 3D hierarchy to match the re-imported data. It can be time consuming.
I want to show a loading popup to assure the user that the program is still functioning. I've investigated a few ways of doing this, including BackgroundWorkers:
public BackgroundWorker _backgroundWorker = new BackgroundWorker();
public void ExportAndReimport()
{
OpenLoadingWindow("Exporting and Reimporting ");
_backgroundWorker.DoWork += new DoWorkEventHandler((s, e) =>
{
Export();
Import();
});
_backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((s, e) =>
{
CloseLoadingWindow();
});
_backgroundWorker.RunWorkerAsync();
}
and async operations:
public async Task ExportAndReimport()
{
OpenLoadingWindow("Exporting and Reimporting ");
await Task.Run(() =>
{
Export();
Import();
CloseLoadingWindow();
});
}
In both cases, the problem is the same: my 3D objects contain many ObservableCollections (since the UI has to represent their properties, among other things). For example, a given object's Children property, which of course is re-populated on reloading the object. I'm not able to make changes to an ObservableCollection from a thread different to the one it was created on.
I just want to freeze input while the load is happening, and show an animated window (ideally, but not necessarily, with a progress bar). What's the best way to achieve this?