我正在使用此代码来检索 url 内容:

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,但我希望能够在 request 上返回结果。我怎样才能等到 DownloadStringAsync 完成请求?

最佳答案

你为什么要等待......这会像以前一样阻塞GUI!

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

client.DownloadStringAsync(uri);

关于c# - DownloadStringAsync 等待请求完成,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5071076/

10-15 03:06