Berryl
Berryl

Reputation: 12863

ui task not acting as expected

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.

Upvotes: 0

Views: 231

Answers (2)

Ian Griffiths
Ian Griffiths

Reputation: 14567

Something like this I think:

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

Upvotes: 1

Anon.
Anon.

Reputation: 60033

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.

Upvotes: 0

Related Questions