DownloadStringAsync()不会异步下载字符串

时间:2011-05-25 02:53:36

标签: c# asynchronous webclient downloadstring

在下载一个字节的数据时,尝试实施downloadStringAsync()以防止用户界面冻结10秒钟。但是,即使下载完成,它也会冻结UI,就像我使用downloadString()

一样

这是我的代码:

    public void loadHTML()
    {
            WebClient client = new WebClient();

            // Specify that the DownloadStringCallback2 method gets called
            // when the download completes.
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(loadHTMLCallback);
            client.DownloadStringAsync(new Uri("http://www.example.com"));
            return;
    }

    public void loadHTMLCallback(Object sender, DownloadStringCompletedEventArgs e)
    {
        // If the request was not canceled and did not throw
        // an exception, display the resource.
        if (!e.Cancelled && e.Error == null)
        {
            string result = (string)e.Result;

            // Do cool stuff with result

        }
    }

1 个答案:

答案 0 :(得分:2)

遇到同样的问题,并找到了解决方案。 这里相当复杂的讨论: http://social.msdn.microsoft.com/Forums/en-US/a00dba00-5432-450b-9904-9d343c11888d/webclient-downloadstringasync-freeze-my-ui?forum=ncl

简而言之,问题是Web客户端正在搜索代理服务器并挂起应用程序。 以下解决方案有助于:

WebClient webClient = new WebClient();
webClient.Proxy = null;
... Do whatever else ...