简体   繁体   English

在异步/等待中,不返回到上下文线程的后果是什么?

[英]In async/await, what are the repercussions of not returning to context thread?

Using async/await as shown here http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html , one of the good practices is using ConfigureAwait(false) so the method return does not have to come back to the request context. 如此处所示,使用async / await http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html ,一种好的做法是使用ConfigureAwait(false)因此方法return不会必须回到请求上下文。 What are the potential consequences of using this? 使用此功能的潜在后果是什么? Asked another way, when would this not be recommended? 问另一种方式,什么时候推荐?

when would this not be recommended? 什么时候推荐?

Your method must return to the same context if there's code further down in the method that depends on that context. 如果方法中依赖该上下文的代码进一步深入,则您的方法必须返回相同的上下文。 If the remainder of that method does not require a particular context, then it's a good practice to use ConfigureAwait(false) . 如果该方法的其余部分不需要特定的上下文,那么使用ConfigureAwait(false)是一个好习惯。

There are two primary examples: UI code and ASP.NET code. 有两个主要示例:UI代码和ASP.NET代码。

UI code must run on the UI thread; UI代码必须在UI线程上运行; this includes most UI widget access, and I extend the definition of "UI code" to include my ViewModels as well (there are some situations in WPF where you can get away with updating the UI from a background thread, but that's not true for all MVVM platforms). 这包括大多数UI窗口小部件访问,并且我还将“ UI代码”的定义扩展到了我的ViewModels(在WPF中有些情况下,您可以避免从后台线程更新UI,但是并非所有情况都如此) MVVM平台)。 So, if your method ends with a textBox1.Text = "" or a myViewModel.MyObservableCollection.Add(4) , then it has to return to the UI thread before it can execute that code. 因此,如果您的方法以textBox1.Text = ""myViewModel.MyObservableCollection.Add(4) ,则它必须返回UI线程才能执行该代码。

ASP.NET code must run in an ASP.NET request context; ASP.NET代码必须在ASP.NET请求上下文中运行; this includes any code that depends on HttpContext.Current (and many System.Web APIs implicitly assume an ASP.NET request context). 这包括依赖于HttpContext.Current任何代码(以及许多System.Web API隐式地假定为ASP.NET请求上下文)。 So, if your method ends with HttpContext.Current.Items... , then it has to return to the ASP.NET request context before it can execute that code. 因此,如果您的方法以HttpContext.Current.Items...结尾,那么它必须返回到ASP.NET请求上下文,然后才能执行该代码。 (Side note: in ASP.NET vNext on .NET 4.6 and higher, the ASP.NET request context is actually going away). (附带说明:在.NET 4.6及更高版本上的ASP.NET vNext中,ASP.NET请求上下文实际上已消失)。

In practice, what this means is that most library code should use ConfigureAwait(false) , since well-written library code does not have a dependency on a particular UI framework or System.Web . 实际上,这意味着大多数库代码应使用ConfigureAwait(false) ,因为编写良好的库代码不依赖于特定的UI框架或System.Web Similarly, most application code should not use ConfigureAwait(false) , since it has to update the UI / send an HTTP response. 同样, 大多数应用程序代码不应使用ConfigureAwait(false) ,因为它必须更新UI /发送HTTP响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM