How to show busy indicator before closing window?

3 posts, 0 answers
  1. Yu
    Yu avatar
    11 posts
    Member since:
    Jul 2010

    Posted 20 Jul 2011 Link to this post

    Hi,

    I have an application that when the user close the window, I'd like to do some time consuming task (update database). At the same time, I want to show busy indicator. When the task is done, close the window as per normal. Here's the sample code
    public partial class MainWindow
        {
            public MainWindow()
            {
                InitializeComponent();
                Closing += MainWindowClosing;
            }
     
            void MainWindowClosing(object sender, CancelEventArgs e)
            {
                e.Cancel = true;
                busyIndicator.IsBusy = true;
                var worker = new BackgroundWorker();
                worker.DoWork += (s, args) => Thread.Sleep(TimeSpan.FromSeconds(3));//just for testing
                worker.RunWorkerCompleted += (s, args) =>
                                                 {
                                                     busyIndicator.IsBusy = false;
                                                     e.Cancel = false;
                                                 };
                worker.RunWorkerAsync();
     
            }
        }

    but it's not working. The "e.Cancel = false" doesn't close the window. Can anyone help? Where did I do wrong?

    Thanks!
  2. Yu
    Yu avatar
    11 posts
    Member since:
    Jul 2010

    Posted 20 Jul 2011 Link to this post

    Found the solution. Add in a flag _taskDone. Here it is:

    public partial class MainWindow
        {
            public MainWindow()
            {
                InitializeComponent();
                Closing += MainWindowClosing;
            }
     
            private bool _taskDone = false;
     
            void MainWindowClosing(object sender, CancelEventArgs e)
            {
                if (!_taskDone)
                    e.Cancel = true;
                busyIndicator.IsBusy = true;
                var worker = new BackgroundWorker { WorkerReportsProgress = true };
                worker.DoWork += (s, args) => Thread.Sleep(TimeSpan.FromSeconds(3));
                worker.RunWorkerCompleted += (s, args) =>
                                                 {
                                                     _taskDone = true;
                                                     Close();
                                                 };
                worker.RunWorkerAsync();
            }
        }

    Thanks to this link How to fade in and out a window in WPF Posted by admin in Programming, WPF
  3. UI for WPF is Visual Studio 2017 Ready
  4. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 25 Jul 2011 Link to this post

    Hello Yu,

    We are glad that you have resolved the issue yourself. Thank you for sharing it.

    Greetings,
    Konstantina
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Back to Top