Hello,
Welcome to our Microsoft Q&A platform!
By checking your code, you used concurrency::task and get() method to do an asynchronous operation. But the get() method is not appropriate for a UI thread, it will block the calling thread until the results of the asynchronous operation are available. So to avoid holding up OS threads from doing other useful work, it's better to use IAsyncOperation and co_return, because you want to return a value after the operation completes.
Before you do compute-bound work in a coroutine, you need to return execution to the caller so that the caller isn't blocked. If you're not already doing that by co_await-ing some other operation, then you can co_await the winrt::resume_background function.
Windows::Foundation::IAsyncAction MainPage::ClickHandler(Windows::Foundation::IInspectable const& /* sender */, Windows::UI::Xaml::RoutedEventArgs const& /* args */)
{
if (MainViewModel().BookSkus().Size() > 5)
{
MainViewModel().BookSkus().Clear();
}
else {
auto stuff{ co_await RetrieveSearchResults() };
MainViewModel().BookSkus().Append(winrt::make(stuff + winrt::to_hstring(MainViewModel().BookSkus().Size())));
}
pring().IsActive(false);// Set loading to true
pring().UpdateLayout();
}
Windows::Foundation::IAsyncOperation< winrt::hstring> MainPage::RetrieveSearchResults()
{
pring().IsActive(true);// Set loading to true
pring().UpdateLayout();
co_await winrt::resume_background();
loopPrime(200000);
co_return L"newbook";
}
Thanks.