C# user interface updates are not deterministic

冷暖自知 提交于 2019-12-04 20:24:38

Let me break it down for you:

  1. TextBlock updates only happen when I step into an await or when the event handler closes.

This is perfectly normal. The await call basicly does the next thing: start the given function, and until that is ongoing return to the callee. This also implies, that when your awaited function returns, the scope immideatly returns to after the await. So, if your awaited function is really quick, then the UI might not have enough time to apply your changes.

  1. Sometimes TextBlock updates occur at await #1, sometimes at await #2, sometimes at both, and sometimes at neither.

Same as the above, sometimes the UI has enough time, sometimes it doesn't. That's the funny thing with threads. They are non-deterministic.

  1. Regardless of prior events, T1 will show "Work done." and T2 will show "" at the end of Btn_Click().

After the DoWork function completes, the UI gains back full control, so it can apply all your changes -> the last Text values get applied.

Edit:

If you really want the UI to update, you can await Task.Yield(). This will force the UI to gain back control.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!