Task.Yield - real usages?

前端 未结 6 2061
-上瘾入骨i
-上瘾入骨i 2020-12-01 05:29

I\'ve been reading about Task.Yield , And as a Javascript developer I can tell that\'s it\'s job is exactly the same as setTime

6条回答
  •  半阙折子戏
    2020-12-01 05:55

    No, it's not exactly like using setTimeout to return control to the UI. In Javascript that would always let the UI update as the setTimeout always has a minimum pause of a few milliseconds, and pending UI work has priority over timers, but await Task.Yield(); doesn't do that.

    There is no guarantee that the yield will let any work be done in the main thread, on the contrary the code that called the yield will often be prioritised over UI work.

    "The synchronization context that is present on a UI thread in most UI environments will often prioritize work posted to the context higher than input and rendering work. For this reason, do not rely on await Task.Yield(); to keep a UI responsive."

    Ref: MSDN: Task.Yield Method

提交回复
热议问题