3

尝试实现在下载一字节数据downloadStringAsync()时防止 UI 冻结 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

        }
    }
4

1 回答 1

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 ...
于 2014-04-05T20:55:34.103 回答