【发布时间】:2012-10-28 01:16:18
【问题描述】:
我有一个方法可以通过 HttpClient 提取一些 HTML,如下所示:
public static HttpClient web = new HttpClient();
public static async Task<string> GetHTMLDataAsync(string url)
{
string responseBodyAsText = "";
try
{
HttpResponseMessage response = await web.GetAsync(url);
response.EnsureSuccessStatusCode();
responseBodyAsText = await response.Content.ReadAsStringAsync();
}
catch (Exception e)
{
// Error handling
}
return responseBodyAsText;
}
我有另一种看起来像这样的方法:
private void HtmlReadComplete(string data)
{
// do something with the data
}
我希望能够调用 GetHTMLDataAsync,然后在读取 html 后让它在 UI 线程上调用 HtmlReadComplete。我天真地认为这可以用某种看起来像的东西来完成
GetHTMLDataAsync(url).ContinueWith(HtmlReadComplete);
但是,我无法获得正确的语法,我什至不确定这是处理它的适当方法。
提前致谢!
【问题讨论】:
标签: c# asynchronous async-await c#-5.0