Donny Andrian
Donny Andrian

Reputation: 55

WinRT/C++ co_await resume_foreground(Dispatcher()) cause try_as runtime error

What causes the try_as() runtime error to occur when calling co_await resume_foreground(Dispatcher()) in the LoadOverallStatisticsAsync() function. try_as() runtime error

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

Answers (0)

Related Questions