【发布时间】:2011-09-01 09:44:27
【问题描述】:
谁能指点我一个教程或提供一些示例代码来调用System.Net.WebClient().DownloadString(url) 方法而不会在等待结果时冻结 UI?
我认为这需要使用线程来完成?有没有我可以使用而无需太多开销代码的简单实现?
谢谢!
实现了 DownloadStringAsync,但 UI 仍然冻结。有什么想法吗?
public void remoteFetch()
{
WebClient client = new WebClient();
// Specify that the DownloadStringCallback2 method gets called
// when the download completes.
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(remoteFetchCallback);
client.DownloadStringAsync(new Uri("http://www.google.com"));
}
public void remoteFetchCallback(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;
MessageBox.Show(result);
}
}
【问题讨论】:
标签: c# multithreading webclient downloadstring