How to show a loading to the user during a heavy operation in C#

Mojtaba_Hakim 281 Reputation points
2022-10-25T16:51:01.417+00:00

I have a C# WPF project I want to use a task to display a spinner (loader) without freezing the UI so that the long process is finished.

What I need: I want to show a loading to the user during a heavy operation

What have I done: Well, I have a spinner that is hidden and when heavy operations start, I show that spinner ,But the UI freezes again! enter image description here

CS:

 private void Button1_Click(object sender, RoutedEventArgs e)  
        {  
            //Showing the loader  
            Task.Factory.StartNew(async () =>  
            {  
                await Dispatcher.InvokeAsync(() =>  
                {  
                    Spinner.Visibility = Visibility.Visible;  
  
                });  
            });  
  
            //Simulation Long Process  
            var TheRsult = dbms.Database.SqlQuery<object>("WAITFOR DELAY '00:00:04' SELECT GETDATE()").FirstOrDefault();  
            Thread.Sleep(500);  
            for (int i = 0; i < 1000; i++)  
            {  
                Console.Write(i);  
            }  
        }  

My Full Source Code :n354bf7ngyspoy5f

Please guide me regarding this issue, thank you

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 81,636 Reputation points
    2022-10-25T17:48:06.627+00:00

    You can do something like this (test with an Image and waiting 5 seconds to simulate a long task) :

        async System.Threading.Tasks.Task MyTask()  
        {  
            await Task.Run(async () =>  
            {  
                await System.Threading.Tasks.Task.Delay(5000);  
                this.Dispatcher.Invoke(() =>  
                {  
                    image1.Visibility = Visibility.Collapsed;  
                });  
                MessageBox.Show("Finished", "Information", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.Yes);  
            });  
        }  
    
        private async void LaunchTask()  
        {            
            image1.Visibility = Visibility.Visible;  
            await MyTask();  
        }
    
    1 person found this answer helpful.

  2. Lex Li (Microsoft) 4,662 Reputation points Microsoft Employee
    2022-10-27T05:51:22.297+00:00

    People often use the BusyIndicator control from Extended WPF Toolkit (free and open source),

    BusyIndicator_busyindicator.jpg

    https://github.com/xceedsoftware/wpftoolkit/wiki/BusyIndicator

    Other control vendors also offer similar things.

    1 person found this answer helpful.
    0 comments No comments