0

I try to automatically shut down an wpf application at midnight with:

Dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);

This works very well in general, but when there is a message box opening waiting for user response, the application fails to shutdown. Is there a way to shut down the application regardless of the opening messagebox?

Bob
  • 381
  • 4
  • 14
  • Thanks, I will try it and report it to you. It is kind of odd because I believe I have being using Application.Current.Shutdown() and then for some reason that I do not remember, I changed to Dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal). – Bob Aug 06 '12 at 15:35
  • Application.Current.Shutdown() works great. I did not know why I change it to Dispatcher.BeginInvokeShutdown(). I will study in detail the differences between these shutdown mechanisms. Thanks a lot. – Bob Aug 08 '12 at 14:06

2 Answers2

1

Maybe you can use Environment.Exit (immediately exits...very naughty to do on a GUI app) or find the MessageBox window and send them a close message, or hook the creation of any native MessageBox Dialogs (...i.e. track the Window handle, so you can then close them).

And a very very naughty way:

Process.GetCurrentProcess().Kill()

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47
  • Thank you for the answers. Environment.Exit(0) seems to work for Consoles and Application.Exit works for WinForm application. Kill the application will make the problem complicit here either because there is a lot of automatic invoked work to do before the shutting down. I will first check Application.Current.Shutdown(), if it does not work, I will try to Force Close of MessageBox as you suggested. Thanks. – Bob Aug 06 '12 at 17:15
1

For WPF Applications use

Application.Current.Shutdown();
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208