尝试实现 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
}
}
最佳答案
遇到了同样的问题,找到了解决办法。 这里的讨论相当复杂: 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 ...
关于c# - DownloadStringAsync() 不异步下载字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6119030/