相关疑难解决方法(0)

使用语句和等待关键字在c#中很好地发挥作用

我有一种情况,我正在async调用返回和IDisposable实例的方法.例如:

HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com"));
Run Code Online (Sandbox Code Playgroud)

现在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;
}
Run Code Online (Sandbox Code Playgroud)

例2

using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
    await this.responseLogger.LogResponseAsync(response);

    return true;
}
Run Code Online (Sandbox Code Playgroud)

c# asynchronous using-statement

96
推荐指数
1
解决办法
1万
查看次数

标签 统计

asynchronous ×1

c# ×1

using-statement ×1