[英]Understanding what multiple configureawait(false) do in a single async method
[英]ConfigureAwait(false) on the single line
在單個代碼行上編寫ConfigureAwait(false)
是否有意義,例如:
private async Task test()
{
await Task.Run(() =>{}).ConfigureAwait(false);
}
如果Microsoft 文檔說ConfigureAwait(false)
僅影響方法“tail”。
async Task MyMethodAsync()
{
// Code here runs in the original context.
await Task.Delay(1000);
// Code here runs in the original context.
await Task.Delay(1000).ConfigureAwait(continueOnCapturedContext: false);
// Code here runs without the original
// context (in this case, on the thread pool).
}
ConfigureAwait(false)
僅對未完成的等待對象(類似Task
的類型)有影響,並且僅在SynchronizationContext
。
第一個簡單的例子,如果ConfigureAwait(false)
被省略:
private async Task test()
{
await Task.Run(() =>{});
}
如果test
方法在SynchronizationContext
下運行,則await
之后的延續將發布到該SynchronizationContext
。
有關更詳細的說明,請參閱ConfigureAwait FAQ 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.