希望有人能帮我解决问题。
我试图使用以下代码获取网站的html代码:
public string DownloadString(string add)
{
string html = "";
using (WebClient client = new WebClient())
{
client.Proxy = null;
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
while (html == "")
{
try
{
html = client.DownloadString(add);
}
catch (WebException e)
{
html = "";
}
}
client.Dispose();
}
return html;
}我需要这个函数中的字符串(Caller):
public HtmlNode get_html(string add)
{
add_val(add);
Uri madd = new Uri(add);
Stopwatch timer = Stopwatch.StartNew();
Task<string> task = Task.Factory.StartNew<string>
(() => DownloadString(add));
string html = task.Result;
//string html = DownloadString(add);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
//doc.Load(new StringReader(html));
doc.LoadHtml(html);
HtmlNode root = doc.DocumentNode;
timer.Stop();
TimeSpan timespan = timer.Elapsed;
label18.Text = String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10);
return root;
}我尝试过html = await client.DownloadStringTaskAsync(new Uri(add));,但它似乎不起作用,下载字符串时它仍然会冻结UI。
提前谢谢你!
发布于 2015-03-25 12:21:20
为了防止阻塞UI,您的代码需要一直是异步的。
特别是这段代码将阻塞UI线程,直到下载完成:
Task<string> task = Task.Factory.StartNew<string>(() => DownloadString(add));
string html = task.Result;您需要的是使用await来代替:
Task<string> task = Task.Run(() => DownloadString(add));
string html = await task;这意味着您的get_html方法必须是async
public async Task<HtmlNode> get_htmlAsync(string add)它的所有调用者都必须使用await,并成为async等等。您必须允许异步在调用树中一直增长。
发布于 2015-03-25 12:33:23
这里的问题是对task.Result的调用总是阻塞的(如果任务尚未准备好,调用线程将等待完成),所以UI线程将被阻塞,等待任务结果。如果您正在使用.Net Framework4 (@Stephen为4.5编写了一个解决方案),您需要做的是以如下方式使用延续。
public void get_html(string add)
{
add_val(add);
Uri madd = new Uri(add);
Stopwatch timer = Stopwatch.StartNew();
Task.Factory.StartNew<string>(() => DownloadString(add))
.ContinueWith(t => {
string html = task.Result;
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
HtmlNode root = doc.DocumentNode;
timer.Stop();
TimeSpan timespan = timer.Elapsed;
label18.Text = String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10);
//
Update UI with results here
//
}, TaskScheduler.FromCurrentSynchronizationContext());
}或者,您可以将get_html返回类型设置为Task<string>或Task<HtmlNode>,以便在其上使用延续。
https://stackoverflow.com/questions/29245259
复制相似问题