question

Kai-4867 avatar image
0 Votes"
Kai-4867 asked LUZSITAGABUTERO-4846 answered

[UWP][CPP] c++ winrt how to update collection after long running async operation and show a loading icon

I am trying to figure out how to show a spinning icon during long operations that update the ui. This is proving to be extremely difficult and unintuitive.

It's a simple need. Go retrieve information. (This can take a minute). While waiting show a loading icon. when done, stop showing the loading icon and update a collection that has changed.

I've prepared a sample project based on the databinding collection example from the docs.

https://github.com/camccar/collecectionasyncexample/tree/master

I use a calculate prime function to simulate long loading times. In real life I will be getting data from a sqlite database. I then add something to a collection. The loading icon is not showing.

How can i update a collection but show a loading icon while waiting?

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

FayWang-MSFT avatar image
1 Vote"
FayWang-MSFT answered LUZSITAGABUTERO-4846 commented

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.






· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Again this works but it only seems to work with windows run time types. In this case a hstring. In the real world I am returning a custom type where this does not work and the documentation is lacking . I get the error "must be WinRT type"

I can try to prepare a more specific example and re ask the question.

Edit. Never-mind. I used a return value of hstring as a signal. I made a singleton that sets the custom type in the back ground. And retrieve that data from the singleton after the async operation is finished. So I have gotten this to work. Thank you for your help.

0 Votes 0 ·

emphasized text
i need some more webinar

0 Votes 0 ·
LUZSITAGABUTERO-4846 avatar image
0 Votes"
LUZSITAGABUTERO-4846 answered

by checking code you uses concurrency task and get the method to do an asynchronous operation.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.