簡體   English   中英

單行上的 ConfigureAwait(false)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM