gpt4 book ai didi

.net - ConfigureAwait 有什么作用? ASP.NET Web API 2

转载 作者:行者123 更新时间:2023-12-01 00:25:02 25 4
gpt4 key购买 nike

我正在研究 ASP.NET Web API 2 应用程序。在我们为每个异步请求使用的应用程序中:

ConfigureAwait(false);

我真的不明白这是什么意思。我在互联网上看了看,但仍然不明白它到底是做什么的?

最佳答案

要了解它,您需要了解什么是Synchronization ContextThreading Models .

从实践的角度来看,让我们看一下规范的例子。您正在开发 GUI 应用程序(Win Forms 或 WPF)。您有专用的 UI 线程。
您必须在 UI 线程上更新 UI,并且不要通过计算或等待某些响应来阻塞 UI 线程。

ConfigureAwait 对 async/await 有意义。默认情况下,await 之后的代码将在捕获的 UI 线程上运行,

await DoAsync();
//UI updating code goes here. Will run on UI thread

如果指定 ConfigureAwait(false),则 await 之后的代码将在线程池上运行
await DoAsync().ConfigureAwait(false);
//CPU intensive operation. Will run on thread pool

只要您有 ASP.NET 应用程序,您就没有专用的 UI 线程。但是同步上下文也很重要。 Understanding the SynchronizationContext in ASP.NET会给你详细信息。

关于.net - ConfigureAwait 有什么作用? ASP.NET Web API 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45756163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com