Transfer of executive power: Task.Yield, Dispatcher.Yield - walterlv , Yi Tsai's blog bead front. Students can jump directly over to see.
Sorry that I did not find in it Task.Yield
a suitable application scenarios. About Task.Yield
scenarios also have to debate, showing that this thing must be very sad. This paper tried Task.Yield
several usage scenarios, see if you can become more elegant code.
Sitting next to MVP unworthy sequel, fear and trepidation. .
ReferenceSource see which Task.Yield
method will return a directYieldAwaitable
If your code is written so
So this time it will sell its own thread, and then come up with a thread from the thread pool to continue the follow-up code
So the question is, what is suitable for useTask.Yield
1. separate thread to do large-scale computing
You might think of using Task.Yield
to open a background thread to do large-scale computing
But in fact, we can use more common Task.Run
instead.
Two way effect on the map in most cases is the same.
So where is not the same place it? The first written in synchronization context will be used in subsequent calculations, while the second does not
Of course, if you are developing WPF program, and that is time-consuming calculation on the UI thread, 可以
choose the first.
Note: Task.Yield
there is no ConfigureAwait
way we can see its design goal is to synchronize the context of the work
2. UI time-consuming operation, anti-Caton
The previous section proposes an operational scenario is through Task.Yield
a "slice" of the UI time-consuming operation to prevent Caton.
But at least for WPF program, here it is clearly better solutions
or
In the UI thread Dispatcher.Yield
and Task.Yield
are equivalent, but Dispatcher.Yield
have an extra refinement of the priority control
3. It's really sad, sad to only grammar optimization
研究之后我坚定了这个方法的鸡肋情况,绞尽脑汁能想到的就只有一些语法层面的简化
例如对于如下的循环中进行简写
这部分的代码简化后还是看着很清晰的
还有一种情况是在接口约束了返回值为Task
的情况下,实现中没有异步时可以使用Task.Yield
满足接口约束
当然你也可以使用Task.FromResult
进行替代
不过这两者存在着是否进行线程切换的细微差别,大部分情况下,更少的线程切换(即Task.FromResult
)是更优的
小结
Task.Yield
可以快速便捷的出让线程的使用权,但是在实际应用上(至少是WPF)缺少较好的使用场景。
参考链接:
- c# – Task.Yield – 真正的用法? - 代码日志
- Dispatcher.Yield – When and how to use it on WinRT – wpdev
- 出让执行权:Task.Yield, Dispatcher.Yield - walterlv
- 终于明白了 C# 中 Task.Yield 的用途 - dudu - 博客园
- c# - When would I use Task.Yield()? - Stack Overflow
- c# - Task.Yield - real usages? - Stack Overflow
- c# - “await Task.Yield()” and its alternatives - Stack Overflow
- Dispatcher.Yield Method (System.Windows.Threading) - Microsoft Docs
- Task.cs
- YieldAwaitable.cs
本文会经常更新,请阅读个人博客原文: https://xinyuehtx.github.io/ ,以避免陈旧错误知识的误导,同时有更好的阅读体验。
This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing. Welcome to reprint, use, repost, but be sure to keep the article signed by Huang Tengxiao (containing links: https://xinyuehtx.github.io/ ), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification. If you have any questions, please contact me .