Reputation: 55
What causes the try_as()
runtime error to occur when calling co_await resume_foreground(Dispatcher())
in the LoadOverallStatisticsAsync()
function.
Note: using resume_foreground(Dispatcher())
is only for the purpose of ensuring that the function runs on the UI Thread.
HomePage.cpp
fire_and_forget HomePage::HomeLoaded(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
loadAction = [lifetime = get_strong()]() -> winrt::Windows::Foundation::IAsyncAction
{
...
co_await lifetime->LoadOverallStatisticsAsync();
...
}();
co_await loadAction;
}
winrt::Windows::Foundation::IAsyncAction HomePage::LoadOverallStatisticsAsync()
{
co_await resume_foreground(Dispatcher()); // try_as() runtime error occurs here
apartment_context ui_thread;
co_await resume_background();
// DoThings();
co_await ui_thread;
// Update UI
}
Edit: this code is run on a UWP project in a WinRT/C++ Desktop environment
Upvotes: 0
Views: 408