且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 ConfigureAwait(false) 和 Task.Run 有什么区别?

更新时间:2023-01-15 20:34:33

当你说 Task.Run 时,你是说你有一些 CPU 工作要做,这可能需要很长时间,所以它应该始终在线程池线程上运行.

When you say Task.Run, you are saying that you have some CPU work to do that may take a long time, so it should always be run on a thread pool thread.

当您说 ConfigureAwait(false) 时,您是说该 async 方法的其余部分不需要原始上下文.ConfigureAwait 更像是一个优化提示;它并不总是意味着延续在线程池线程上运行.

When you say ConfigureAwait(false), you are saying that the rest of that async method does not need the original context. ConfigureAwait is more of an optimization hint; it does not always mean that the continuation is run on a thread pool thread.