使用WebClient.OpenReadAsync双下载

时间:2012-05-11 15:21:19

标签: windows-phone-7 asynchronous webclient

好的,在我的应用程序中,我需要下载两个数据列表来详细说明,但我不知道该怎么做..

我单击一个按钮然后我认为下载几乎一起开始。这对我有好处,不好的是我的应用程序在做其他任何事情之前无法理解如何等待下载..

我知道有一个设计问题,但我无法弄清楚如何解决它..

代码是(或多或少)这样的东西:

private void button_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            WebClient webClient = new WebClient();

            Uri uri = new Uri("http://myRESTservice");
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync(uri); //this will set a private variableA

            dwnl();          

            doSomething(); //this will do something with A and B


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


private void dwnl()
    {
        try
        {
            WebClient webClient = new WebClient();

            Uri uri = new Uri("http://myRESTservice/anotherAddress");
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted_B);
            webClient.OpenReadAsync(uri); //this will set a private variableB

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

希望你能理解这个问题..

1 个答案:

答案 0 :(得分:2)

当您的应用程序正在下载数据时,即已调用OpenReadAsync方法时,您可能会显示忙碌指示。然后,您的doSomething方法将从OpenReadCompleted事件处理程序中调用。 如果您希望在另一个完成后进行一次下载,那么您也可以在OpenReadCompleted事件处理程序中调用dwnl方法。