【发布时间】:2013-05-10 02:43:18
【问题描述】:
我有一种情况,我正在对一个返回的方法和IDisposable 实例进行async 调用。例如:
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com"));
现在在async 出现之前,当使用IDisposable 实例时,这个调用和使用“response”变量的代码将被包装在一个using 语句中。
我的问题是,当async 关键字被混用时,这是否仍然是正确的方法?即使代码编译了,下面两个示例中的 using 语句是否仍能按预期工作?
示例 1
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
// Do something with the response
return true;
}
示例 2
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
await this.responseLogger.LogResponseAsync(response);
return true;
}
【问题讨论】:
标签: c# asynchronous using-statement