views:

25

answers:

2

In the code below, I would like to display a status message while fetching some data before and not display a dialog populated with that data until the data fetch is complete. But the dialog is instead being displayed before the data gets there.

What am I doing wrong?

Cheers,
Berryl

ProjectSelectionViewModel vm = null;
SetStatus("Fetching data...");
var task = Task.Factory.StartNew(() =>
    {
        vm = presentationFactory.GetProjectSelectionViewModel();
    }
                            );
task.ContinueWith(t => SetStatus("Finished!!!"), TaskScheduler.FromCurrentSynchronizationContext());
var userAction = uiService.ShowDialog(Strings.ViewKey_ProjectPicker, vm);
// etc.
A: 

You code executes the fetch asynchronously, but carries on with showing the completion dialogue without waiting for the async call to finish.

You should be invoking the continuation in a callback from the fetch, rather than in the same method that actually initiates the request.

Anon.
you mean in the continuation (something like task.ContinueWith(t=>_onFetchFinished(vm), ...) or in the creation of the task (something like Task.Factory.StartNew(() =>_fetch(), ()=>_onFetchFinished(vm). Can you scratch out some quick and dirty code please?
Berryl