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

标签 c# asynchronous async-await task

我正在尝试同步运行异步任务,并且想知道 .RunSynchronously() 之间有什么区别和GetAwaiter().GetResult()是。

我见过很多 .Wait() 之间的比较以及这两种方法,但没有将其中一种与另一种进行比较。

最佳答案

RunSyncronously 表示使用当前调度程序在当前线程上运行委托(delegate)。但是,这适用:

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

另一方面,

WaitGetAwaiter().GetResult() 根本不安排 Task,它只是阻止调用线程直到任务完成。如果从单线程同步上下文调用此操作可能会死锁。

MSDNDocs

关于c# - .RunSynchronously() 和 GetAwaiter().GetResult() 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48932626/

相关文章:

node.js - 需要一个正确的 eslintrc 用于 async/await - 使用 7.6+ nodejs

c# - 模拟对象列表

c# - 在 C# MVC 中的单个 Bundle 中添加多个 Bundle

node.js - async.parallel 和 async.series 有什么区别?

google-chrome - 缺少 Google Chrome 开发工具上的异步复选框

node.js - 为什么我的函数由异步函数组成,没有返回正确的值? ( Node JS)

c# - 导入 pdf 中的指定目的地

c# - 检测日文字符输入和 "Romajis"(ASCII)

java - 返回由异步方法返回的对象

javascript - 异步/等待返回结果不 promise