gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:13:35 24 4
gpt4 key购买 nike

我正在尝试同步运行异步任务,并且想知道 .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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com