简体   繁体   中英

Best way to wait async to complete on WP7

i having some hard time on this, i'm trying to get my first WP7 app out. I have an method that download the html from a website and regex it, but the problem is, when i click the button for the first time, nothing happens, on the second try, it fills the grid perfectly, when i was debugging i saw the string with the HTML is already assigned correctly before the method even starts. So, the question is, what is the simplest way to wait for async method to finish? I've searched about CTP async and some other ways, but i can't manage to make it work. Here's is the code

   public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        doc = e.Result;
    }

    public static List<Row> Search(string number)
    {
        WebClient wClient = new WebClient();

        sNumber = number;
        int i = 0;
        DateTime datetime;

        wClient.DownloadStringAsync(new Uri(sURL + sNumber));
        wClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
              /*More code*/
     }

The button calls the method Search() and uses the list returned to fill the grid.

The wClient.DownloadStringAsync(new Uri(sURL + sNumber)); method executes after all the code in that method is executed.

1) At first doc is null

2) Then you call wClient.DownloadStringAsync(new Uri(sURL + sNumber)); but doesn't execute!!

3) Then you return doc (which is still null)

4) After all this, wClient.DownloadStringAsync(new Uri(sURL + sNumber)); is executed and doc is filled.

That is why when you press the Search button for the 2nd time, the grid is filled perfectly

NB You must register the DownloadStringCompletedEventHandler before calling the async method. And you only have to register this event handler once, ie in the constructor, because you are adding an event handler everytime this method is executed. So if you press the Search button for 5 times, the grid is filled for 5 times although you don't notice

One solution would be:

Here's is the code

   public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result != null)
        {
            //populate grid view
        }
    }

    public static void Search(string number)
    {
        WebClient wClient = new WebClient();

        sNumber = number;
        int i = 0;
        DateTime datetime;

        wClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); //this should be added in the constructor, so it would only be added once
        wClient.DownloadStringAsync(new Uri(sURL + sNumber));
     }

There's at least two problems in your code: you need to subscribe to the DownloadStringCompleted before calling DownloadStringAsync - otherwise there is a chance that the download completes before you subscribe. Also since your completion method is really short you can do this inline using a lambda.

Secondly your method is asynchronous - it will not return a List<Row> since the web call executes asynchronously. You will have to populate your grid in your completion method and make your method return void. This is why it "works" the second time - the now completed results of the first call are returned.

wClient.DownloadFileCompleted += (sender, e) =>
{
    //you should also do error checking here
    //populate grid 
};
wClient.DownloadStringAsync(new Uri(sURL + sNumber));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM