question

Kai-4867 avatar image
0 Votes"
Kai-4867 asked ·

[UWP][cpp] c++ winrt uwp show loading during long operations.

given a ProgressRing in the xaml such as,

ProgressRing x:Name="MyLoadingRing"

What is the recommended way to set this value to true, update the UI and then do a long running operation. For example a sqlite query.

I've tried

 MyLoadingRing().IsActive(true);
 MyLoadingRing().UpdateLayout();


This doesn't work. Do I need to make the long running operation Async? Seems like there should be a example of how to do this in c++winrt as it would be a very common need.

Edit: An example project that demonstrates the problem can be found here https://github.com/camccar/longloading


windows-uwp
· 2
10 |1000 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.

When I used the code, the ProgressRing can display well. So what do you mean This doesn't work, the ProgressRing didn't display when you use .IsActive(true)? If yes, can you provide more code snippet for us to reproduce? If it is not, can you describe more details about what's your expected behavior and what's behavior you currently get?

0 Votes 0 ·

Hi Fay and yes, I made a simple example project to demonstrate here https://github.com/camccar/longloading

The readme explains the problem in more detail.

0 Votes 0 ·

1 Answer

FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered ·

Hello,​


Welcome to our Microsoft Q&A platform!​

By checking your code, the ProgressRing you used is correct and the issue occurs because the for loop blocks the thread, it will be blocked until the function stops. So, before you do compute-bound work in a coroutine, you need to return execution to the caller (in other words, introduce a suspension point) 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. For updating UI, you can co_await the winrt::resume_foreground function to switch to a specific foreground thread. For more details about concurrency and asynchrony, you can refer to this document.

 #include "winrt/Windows.UI.Core.h"
    
 Windows::Foundation::IAsyncAction MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
 {
     pring().IsActive(true);// Set loading to true
     pring().UpdateLayout();
     myButton().Content(box_value(L"Clicked"));
     co_await winrt::resume_background();
     loopPrime(500000); //Long running operation here
     co_await winrt::resume_foreground(pring().Dispatcher());
     pring().IsActive(false);// Set to false when done
 }


Thanks










· 1 · Share
10 |1000 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.

This simple example works, but if my real app i'm not seeing my models get updated. Seems like there is more threads. I'll have to think of a more complicated way to ask it to show the whole problem.

0 Votes 0 ·