简体   繁体   English

Is.GetAwaiter().GetResult(); 一般用途安全吗?

[英]Is .GetAwaiter().GetResult(); safe for general use?

I read in a few places that .GetAwaiter().GetResult();我在几个地方.GetAwaiter().GetResult(); could cause deadlocks and that we should use async / await instead.可能会导致死锁,我们应该改用async / await But I see many code samples where this is used.但是我看到很多使用它的代码示例。 Is it ok to use it?可以用吗? Which are the cases where it can deadlock?在哪些情况下会出现死锁? Is there something else I should use, like Task.Wait ?还有其他我应该使用的东西吗,比如Task.Wait

As I describe on my blog, GetAwaiter().GetResult() can deadlock when it's used in a one-thread-at-a-time context .正如我在我的博客中所描述的, GetAwaiter().GetResult()在一个线程一次上下文中使用时可能会死锁 This is most commonly seen when called on the UI thread or in an ASP.NET context (for pre-Core ASP.NET).这在 UI 线程或 ASP.NET 上下文中调用时最常见(对于 pre-Core ASP.NET)。

Wait has the same problems. Wait也有同样的问题。 The appropriate fix is to use await , and make the calling code asynchronous.适当的解决方法是使用await ,并使调用代码异步。

Note that the Main method in Console apps is an exception to this rule;请注意,控制台应用程序中的Main方法是此规则的一个例外; it is perfectly appropriate to use there.在那里使用非常合适。 Many code samples use it in this way.许多代码示例以这种方式使用它。

When considering these kinds of things, it always helps to look at the official documentation .在考虑这些事情时,查看官方文档总是有帮助的。 In the remarks section you will find this:在备注部分,您会发现:

This method is intended for compiler use;此方法供编译器使用; do not use it directly in your code.不要直接在您的代码中使用它。

I think that gives a fairly strong steer on this.我认为这对此提供了相当有力的指导。 It basically gives the developers of.Net a licence to change how it works in the future.它基本上为 .Net 的开发人员提供了在未来更改其工作方式的许可。 They may never do so, but I wouldn't be confident myself.他们可能永远不会这样做,但我自己不会有信心。 As for what else you should be using it would depend upon the context, but async/await is the most likely alternative.至于你还应该使用什么取决于上下文,但 async/await 是最有可能的选择。

暂无
暂无

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

相关问题 GetAwaiter()。GetResult()与GetAwaiter()? - GetAwaiter().GetResult() vs GetAwaiter()? .RunSynchronously()和GetAwaiter()之间的区别.GetResult()? - Difference between .RunSynchronously() and GetAwaiter().GetResult()? .Wait() 与 GetAwaiter().GetResult() 之间有什么区别? - What is the difference between .Wait() vs .GetAwaiter().GetResult()? 异步配置 DBContext 以避免 TokenAcquisition 中的 GetAwaiter().GetResult() - Configure the DBContext Asyncronously to avoid GetAwaiter().GetResult() in TokenAcquisition 异步等待 vs GetAwaiter().GetResult() 和回调 - Async await vs GetAwaiter().GetResult() and callback Task.Result 是否与 .GetAwaiter.GetResult() 相同? - Is Task.Result the same as .GetAwaiter.GetResult()? AsyncFunction().GetAwaiter().GetResult() 和 Task.Run(() => AsyncFunction).GetAwaiter().GetResult() 有什么区别? - What's the difference between AsyncFunction().GetAwaiter().GetResult() and Task.Run(() => AsyncFunction).GetAwaiter().GetResult()? 无法在 GetAwaiter().GetResult() 上将类型“void”隐式转换为“object” - Cannot implicitly convert type 'void' to 'object' on GetAwaiter().GetResult() Task.GetAwaiter()。GetResult()在Windows服务中导致死锁 - Task.GetAwaiter().GetResult() causing a deadlock in a Windows Service 将 GetAwaiter().GetResult() 与 ServiceBusReceiver.PeekMessagesAsync 一起使用时出现 System.InvalidOperationException - System.InvalidOperationException when using GetAwaiter().GetResult() with ServiceBusReceiver.PeekMessagesAsync
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM