DownloadStringAsync等待请求完成 [英] DownloadStringAsync wait for request completion

查看:589
本文介绍了DownloadStringAsync等待请求完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code来检索URL内容:

I am using this code to retrieve an url content:

private ArrayList request(string query)
{
    ArrayList parsed_output = new ArrayList();

    string url = string.Format(
        "http://url.com/?query={0}",
        Uri.EscapeDataString(query));

    Uri uri = new Uri(url);

    using (WebClient client = new WebClient())
    {
        client.DownloadStringAsync(uri);
    }

        // how to wait for DownloadStringAsync to finish and return ArrayList
    }

我想用 DownloadStringAsync ,因为 DownloadString 挂程序的GUI,但我希望能够返回结果在要求。我怎样才能等到 DownloadStringAsync 完成请求?

I want to use DownloadStringAsync because DownloadString hangs the app GUI, but I want to be able to return the result on request. How can I wait until DownloadStringAsync finish the request?

推荐答案

为什么要等待...将像以前一样挡住GUI!

why would you want to wait... that will block the GUI just as before!

var client = new WebClient();
client.DownloadStringCompleted += (sender, e) => 
{
   doSomeThing(e.Result);
};

client.DownloadStringAsync(uri);

这篇关于DownloadStringAsync等待请求完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆