简体   繁体   English

.RunSynchronously()和GetAwaiter()之间的区别.GetResult()?

[英]Difference between .RunSynchronously() and GetAwaiter().GetResult()?

I'm trying to run an asynchronous task synchronously and was wondering what the differences between .RunSynchronously() and GetAwaiter().GetResult() were. 我正在尝试同步运行异步任务,并想知道.RunSynchronously()GetAwaiter().GetResult()之间的区别是什么。

I've seen a lot of comparisons between .Wait() and these two methods, but nothing comparing one with the other. 我在.Wait()和这两种方法之间看到了很多比较,但没有比较一种方法。

RunSyncronously indicates to run the delegate on the current thread with the current scheduler. RunSyncronously指示使用当前调度程序在当前线程上运行委托。 However, this applies: 但是,这适用于:

If the target scheduler does not support running this task on the calling thread, the task will be scheduled for execution on the scheduler, and the calling thread will block until the task has completed execution 如果目标调度程序不支持在调用线程上运行此任务,则将调度该任务以在调度程序上执行,并且调用线程将阻塞,直到任务完成执行

Wait or GetAwaiter().GetResult() on the other hand doesn't schedule a Task at all, it simply blocks the calling thread until the task completes. WaitGetAwaiter().GetResult()另一方面, GetAwaiter().GetResult()根本不调度Task ,它只是阻塞调用线程,直到任务完成。 This operation can deadlock if called from a single threaded synchronization context. 如果从单线程同步上下文调用,此操作可能会死锁。

MSDN and Docs MSDN文档

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

相关问题 .Wait() 与 GetAwaiter().GetResult() 之间有什么区别? - What is the difference between .Wait() vs .GetAwaiter().GetResult()? GetAwaiter()。GetResult()与GetAwaiter()? - GetAwaiter().GetResult() vs GetAwaiter()? Is.GetAwaiter().GetResult(); 一般用途安全吗? - Is .GetAwaiter().GetResult(); safe for general use? 异步等待 vs GetAwaiter().GetResult() 和回调 - Async await vs GetAwaiter().GetResult() and callback Task.Result 是否与 .GetAwaiter.GetResult() 相同? - Is Task.Result the same as .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 尝试调用异步方法 - await vs GetAwaiter().GetResult(); - Trying to call async method - await vs GetAwaiter().GetResult(); 无法在 GetAwaiter().GetResult() 上将类型“void”隐式转换为“object” - Cannot implicitly convert type 'void' to 'object' on GetAwaiter().GetResult() 不期待的Task.Run和Task.RunSynchronously之间有什么区别 - What is the difference between a non-awaited Task.Run and Task.RunSynchronously
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM