[英]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. Wait
或GetAwaiter().GetResult()
另一方面, GetAwaiter().GetResult()
根本不调度Task
,它只是阻塞调用线程,直到任务完成。 This operation can deadlock if called from a single threaded synchronization context. 如果从单线程同步上下文调用,此操作可能会死锁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.