views:

370

answers:

3

Hi there,

I have some code that works as follows:

App.xaml calls a SetUp() method which populates a local collection using async calls and exposes the collection as a public property.

That's all good.

Now I make an instance of the first page in my Silverlight app like so

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        this.RootVisual = new Summary();
    }

In the constructor of Summary() I expect those async calls to have been complete and my collection to be filled but the async calls have not yet complete. Even if I do a Thread.Sleep(100000....) before i make an instance on Summary() this is the case

And the thing is that until the constructor of Summary() is exited and the UI displayed to the user my async calls do not get kicked off!

What!!!

Is there anything I can do about that or is that just the way asyn calls work i.e. they wait until the current until of work finished before firing?

A: 

Silverlight was specifically designed to not have any blocking operations -- that's why you have to do async in the first place. What you really have to do is make your app run properly while waiting for the async completion handler to execute.

Gabe
A: 

This is by design. The alternative would be that the user has to wait longer before he sees anything at all.

If you really want to prevent showing an incomplete summery then first shown another page with 'waiting' or a progress bar. Fire the async events from that page or its parent. Then show the Summary when the async call returns.

To deal with an incomplete Summary you might want to use ICommand

Paul
+1  A: 

This is how I work round this situation (I'll use simple string download as an example):-

private void Application_Startup(object sender, StartupEventArgs e)
{
    WebClient web = new WebClient();
    web.DownloadStringCompleted += (s, args) =>
    {
        // Do stuff with args.Result);
        this.RootVisual = new Summary();
    };
    web.DownloadStringAsync(new Uri("SomeUrl.txt", UriKind.Relative));
}

Note that the construction of Summary and the assignment to RootVisual are defered until the asynchronous download is complete.

AnthonyWJones