This is a migrated thread and some comments may be shown as answers.

How to show busy indicator before closing window?

2 Answers 264 Views
BusyIndicator
This is a migrated thread and some comments may be shown as answers.
Yu
Top achievements
Rank 1
Yu asked on 20 Jul 2011, 09:08 AM
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 Answers, 1 is accepted

Sort by
0
Yu
Top achievements
Rank 1
answered on 20 Jul 2011, 09:16 AM
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
0
Konstantina
Telerik team
answered on 25 Jul 2011, 11:51 AM
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!

Tags
BusyIndicator
Asked by
Yu
Top achievements
Rank 1
Answers by
Yu
Top achievements
Rank 1
Konstantina
Telerik team
Share this question
or