How can I report progress of an operation to the UI, ..whilst in that operation? i.e, I want to do something like :
void DoExpensiveOperation()
{
var dlg = new ShowProgressDialog();
dlg.ShowDialog();
dlg.Status = "StepOne";
dlg.Progress = "10%";
DoStepOne();
DoStepTwo();
dlg.Status = "StepTwo";
dlg.Progress = "70%";
DoLastStep();
dlg.Status = "LastStep";
dlg.Progress = "100%";
dlg.Close();
}
Clearly this won't work for a number of reasons. I set about trying to resolve this by using a background worker. I do all the work on a background thread, and then simply dispatch the "begin", "progressChange" and "end" events to the UI, to "ShowDialog", "report progress" and "close Dialog" respectively.
- When the DoWork event is raised, it dispatches a call to the UI thread to show the dialog
- It then continues to perform the work in the background thread
- Progress is reported by dispatching progress notifications onto the UI thread
- When the RunWorkerCompleted is raised, a call is dispatched to the UI thread to close the dialog
This works, ... until I want to modify a collection that doesn't support modifications on a non-UI thread, such as the ObservableCollection. I don't want to dispatch these modifications onto the UI thread as then it will just return, hiding how long things are taking (events triggered by a collection change can take a while), making the dialog pointless.