我想在一个单独的线程中执行HttpWebRequest
。
例如我有:
public string GetPage(string url)
{
string html = new WebClient().DownloadString(url);
return html;
}
如何在一个单独的线程中运行它而不会有太多麻烦?
我尝试过启动一个简单的线程,但我不知道如何返回HTML。
答案 0 :(得分:0)
要做到这一点"没有太多麻烦",请使用c#5.0
public async void DoWork(string url)
{
string page = await new WebClient().DownloadStringTaskAsync(url);
//Caller thread will not be blocked but you will be able to
//continue your work at this point after BG thread
//(executing "DownloadStringTaskAsync") has downloaded the page
}
答案 1 :(得分:0)
“没有太多麻烦”不太可能......
选项: