我正在尝试同步运行异步任务,并且想知道 .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
Wait
或 GetAwaiter().GetResult()
根本不安排 Task
,它只是阻止调用线程直到任务完成。如果从单线程同步上下文调用此操作可能会死锁。
关于c# - .RunSynchronously() 和 GetAwaiter().GetResult() 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48932626/