C# - How not make WPF UI Freeze while elaborating

babluplanet 21 Reputation points
2021-06-24T10:01:59.587+00:00

I have a button that after I click it send a lot of data in a remote database with a loop, but during this operation whole wpf UI is freezing. My goal is to make the loader work while it is processing everything with the database. My button code:

 private void btn_Start_Click(object sender, RoutedEventArgs e)
        {
            pb_loader.IsIndeterminate = true; //<- it has to start to make animation

            IEmailService emailService = new EmailService();
            IUserQueryService emailQueryService = new UserQueryService();
            var idIniziale = int.Parse(txtIdIniziale.Text);
            var idFinale = int.Parse(txtIdFinale.Text);
            var n = idFinale - idIniziale;
            string mail = "";
            for(int i=0; i<=n; i++)
            {
                mail = txtMail.Text + idIniziale + "@mail.local";
                var exist = emailQueryService.CheckUserExist(mail); //<- db operation method
                if (exist == false)
                {
                   var lastUniqueId = emailQueryService.GetLastUniqueId();//<- db operation method
                   lastUniqueId = lastUniqueId + 1;
                   var idUtente = emailService.SalvaUtente(mail, lastUniqueId); //<- db operation method
                   emailService.AssegnaReferente(idUtente, txtMail.Text);//<- db operation method
                   emailService.AssegnaRuoli(idUtente); //<- db operation method

                }
                idIniziale++;
            }
            pb_loader.IsIndeterminate = false; //<- it has to end animation of loading
        }
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,710 questions
{count} votes

1 answer

Sort by: Most helpful
  1. sophiamartin2342 0 Reputation points
    2024-07-03T10:00:03.1666667+00:00

    To prevent your WPF (Windows Presentation Foundation) UI from freezing during lengthy operations, you need to ensure that these operations do not run on the UI thread. Here are some strategies to achieve this:

    Use Asynchronous Programming

    Task-Based Asynchronous Pattern (TAP): Use async and await keywords to perform operations asynchronously. This keeps the UI responsive while the operation completes.

    csharpCopy code
    private
    

    BackgroundWorker: This is an older approach but still valid for performing background operations. It provides progress reporting and completion handling.

    csharpCopy code
    private
    

    Use Dispatcher to Update UI

    When you need to update the UI from a background thread, use the Dispatcher to marshal the update back to the UI thread.

    csharpCopy code
    private
    

    Summary

    Use async and await for asynchronous operations. Use BackgroundWorker for background processing with progress reporting. Use Dispatcher to update the UI from a background thread.

    By following these strategies, you can keep your WPF UI responsive and prevent it from freezing during long-running operations.To prevent your WPF (Windows Presentation Foundation) UI from freezing during lengthy operations, you need to ensure that these operations do not run on the UI thread. Here are some strategies to achieve this:

    Use Asynchronous Programming

    Task-Based Asynchronous Pattern (TAP): Use async and await keywords to perform operations asynchronously. This keeps the UI responsive while the operation completes.

    csharp
    private
    

    BackgroundWorker: This is an older approach but still valid for performing background operations. It provides progress reporting and completion handling.

    csharp
    private
    

    Use Dispatcher to Update UI

    When you need to update the UI from a background thread, use the Dispatcher to marshal the update back to the UI thread.

    csharp
    private
    

    Summary

    Use async and await for asynchronous operations.

    Use BackgroundWorker for background processing with progress reporting.

    Use Dispatcher to update the UI from a background thread.

    By following these strategies, you can keep your WPF UI responsive and prevent it from freezing during long-running operations.

    0 comments No comments