How to perform long run operation with ActivityIndicator

Dani_S 3,336 Reputation points
2024-04-22T07:35:47.1633333+00:00

Hi,

How to perform long run operation with ActivityIndicator?

Thanks,

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2024-04-23T05:45:05.2166667+00:00

    Hello,

    If you have a Async/Await method to perform long run operation.

    And you added the <ActivityIndicator x:Name="MyAi" /> in your layout.

    If you have a button, then click the button to download file. Before you start download the file, set IsRunning = true;, If downloaded operation finished (I add Task.Delay(6000) to simulate downloaded operation), use ContinueWith and set IsRunning = false to make the <ActivityIndicator > disappear the in the MainThread.

    private async void DownloadBtn_Clicked(object sender, EventArgs e)
    {
         MyAi.IsRunning = true;
         await Task.Delay(6000).ContinueWith(t => {
    
    
            MainThread.InvokeOnMainThreadAsync(() => {
                 MyAi.IsRunning = false;
             });
    
    
        });
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more