RunAsync - How do I await the completion of work on the UI thread?

前端 未结 4 697
野趣味
野趣味 2020-12-05 08:42

When awaiting Dispatcher.RunAsync the continuation occurs when the work is scheduled, not when the work has completed. How can I await the work completing?

4条回答
  •  自闭症患者
    2020-12-05 09:12

    Your question is assuming that you want to schedule (and wait for) work on a UI thread from a background thread.

    You'll usually find your code is much cleaner and easier to understand (and it will definitely be more portable) if you have the UI be the "master" and the background threads be the "slaves".

    So, instead of having a background thread await some operation for the UI thread to do (using the awkward and unportable Dispatcher.RunAsync), you'll have the UI thread await some operation for the background thread to do (using the portable, made-for-async Task.Run).

提交回复
热议问题