Using a View Model didn’t fix the multi-threaded UI problem I’m having, of course, so another try.
Links:
- Concurrency and asynchronous operations with C++/WinRT
https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/concurrency - Advanced concurrency and asynchrony with C++/WinRT
https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/concurrency-2
The code above throws a winrt::hresult_wrong_thread exception, because a TextBlock must be updated from the thread that created it, which is the UI thread. One solution is to capture the thread context within which our coroutine was originally called. To do that, instantiate a winrt::apartment_context object, do background work, and thenco_await
the apartment_context to switch back to the calling context.
