【发布时间】:2018-02-16 01:28:11
【问题描述】:
我正在学习 async/await 并且在阅读了这篇文章后Don't Block on Async Code
还有这个Is async/await suitable for methods that are both IO and CPU bound
我注意到@Stephen Cleary 的文章中的一个提示。
使用 ConfigureAwait(false) 来避免死锁是一种危险的做法。您必须对阻塞代码调用的所有方法的传递闭包中的每个等待使用 ConfigureAwait(false),包括所有第三方和第二方代码。使用 ConfigureAwait(false) 避免死锁充其量只是一种技巧。
正如我在上面所附的那样,它再次出现在帖子的代码中。
public async Task<HtmlDocument> LoadPage(Uri address)
{
using (var httpResponse = await new HttpClient().GetAsync(address)
.ConfigureAwait(continueOnCapturedContext: false)) //IO-bound
using (var responseContent = httpResponse.Content)
using (var contentStream = await responseContent.ReadAsStreamAsync()
.ConfigureAwait(continueOnCapturedContext: false)) //IO-bound
return LoadHtmlDocument(contentStream); //CPU-bound
}
据我所知,当我们使用 ConfigureAwait(false) 时,其余异步方法将在线程池中运行。为什么我们需要将它添加到每个等待中 在传递闭包中?我自己只是认为这是我所知道的正确版本。
public async Task<HtmlDocument> LoadPage(Uri address)
{
using (var httpResponse = await new HttpClient().GetAsync(address)
.ConfigureAwait(continueOnCapturedContext: false)) //IO-bound
using (var responseContent = httpResponse.Content)
using (var contentStream = await responseContent.ReadAsStreamAsync()) //IO-bound
return LoadHtmlDocument(contentStream); //CPU-bound
}
这意味着在 using 块中第二次使用 ConfigureAwait(false) 是没有用的。请告诉我正确的方法。 提前致谢。
【问题讨论】:
标签: c# multithreading asynchronous