有人可以向我指出一个教程或提供一些示例代码来调用该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);
}
}