This project has moved and is read-only. For the latest updates, please go here.

Handling asynchronous actions

Feb 2, 2012 at 10:40 PM

I have a view + viewmodel which appears as a dialog box.  The view has a few combo boxes.  In the ViewModel I have an override of OnImportsSatisifed which uses an [Import]ed DataService to populate the ComboBoxes (for example, one box has a list of all staff members, about 20 names).  I also have an override of ActivateView which is passed the parameters needed to initialize the View.

My problem is that OnImportsSatisfied is called first and then ActivateView is called ... but ... the DataService is asynchronous (of course) and does not return prior to ActivateView being called.  This means that this first call to ActivateView does not have access to the reference data.  For example, the parameter passed to ActivateView may have a particular staff member who should be selected in the combo box but the items have not been loaded yet.

Does this make sense?  Closing the dialog box and then reopening it works just fine because the DataService has returned by this stage and the combo boxes are loaded.

How should I handle this sort of thing - a different action (ActivateView) depends on an asynchronous action (OnImportsSatisfied) being complete?

At the moemnt I have some flags set up that both ActivateView and OnImportsSatisfied check and set to see who is finished first and decide when work should be done.  This feels very clunky and I probably have a race condition that I haven't spotted.

Any advice?

Feb 3, 2012 at 1:44 AM

Why not create a short workflow? The first IWorkflow step does the load for the comboboxes, and the second step sets the defaults. In the import satisfied call you can kick off the workflow. The activate view will simply assign the values to a local variable. When the call to the combo boxes completes, it takes the local value and uses that to set the synchronized property. If you're concerned about timing, the activate view can check for loaded or not and then assign accordingly. Does that make sense? Am I missing something? If you are comfortable sharing code I can be more specific.