Justin
Justin

Reputation: 6549

Silverlight waiting for asynchronous call

I have a silverlight application that went it starts up, it needs to read a config file that a webservice returns.

So, in my main page, I want something like this:

public MainPage()
{
    InitializeComponent();
    Config cfg = new Config();
    XDocument config = cfg.getConfig();
    //doing stuff with config here
    ...
}

The constructor for config calls readConfigAsnc and I have a method for the readcompleted that returns the xdocument. I want the readConfigCompleted called before execution continues in MainPage(). What is the best way to go about doing this?

Upvotes: 0

Views: 497

Answers (2)

DanTheMan
DanTheMan

Reputation: 3277

The best way is to separate this out into two methods. Pass a function as a parameter of the getConfig, so like this:

cfg.getConfig( fcnToCall );

Later, in your code,

void fcnToCall( XDocument config )
{
    //Do stuff with config here...
}

Another option would be to use lambda expression if you want to retain your local variables:

Config cfg = new Config();
cfg.Callback += new Action<XDocument> action = s => 
        {
            XDocument cfg = s as XDocument;
            //Do stuff with config here...
        };
cfg.getConfig();

Upvotes: 4

Mike Kshymensky
Mike Kshymensky

Reputation: 111

Why not separate out the methods? Instead of having all of this happen in the MainPage(), have the 'Do Stuff' happen in the GetConfigCompleted event.

Upvotes: 3

Related Questions