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

问题描述

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

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,但我希望能够在 request 上返回结果.我怎样才能等到 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 等待请求完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 17:23