I cannot seem to get OnMainWindowClose, OnLastWindowClose, or OnExplicitShutdown to work in my WPF C# gui application. I have changed the name of my main window to UI. I have an entry in my App.xml x:Class as ShutdownMode="OnMainWindowClose". I am using MahApps. I am running the app in debug mode in VS2012 and when I click on the close button the window title bar (Red box with white cross in top right hand corner of window) I can see the window closes but the app is still running in the background. I have to press the debug stop button in VS2012 to kill the process. I am still trying my best to understand this as a beginner so if there is anyone that can suggest me trying something it would be much appreciated.
Asked
Active
Viewed 365 times
0
-
Have you tried everything in the second comment on this thread?: http://stackoverflow.com/questions/9992119/wpf-app-doesnt-shut-down-when-closing-main-window – Asnivor Dec 05 '16 at 15:40
-
Yes, I have tried all the suggestions on the page and none were successful. One of the suggestions I had a few problems getting it to parse so not fully tried: If the above didn't work (I guess you wrote the WPF application from scratch), the main window probably isn't known to the application as the main window. So specify that as-well. In Code Application.MainWindow's value is your Window: MainWindow = mainWindow; In XAML MainWindow = "mainWindow"; I believe I should be replacing mainWindow with my new name of UI but VS2012 always complained about UI – Paul Ruddlesdin Dec 05 '16 at 15:55
-
When you run the application outside of Visual Studio does it close correctly (i.e. there is no lingering process in task manager). If so it could be one of the VS debugging options holding things up. – Asnivor Dec 05 '16 at 16:10
-
Yes, thank you. I was chasing a ghost. It runs and terminates correctly outside of VS. It must be the way debug is configured. I will get round to figuring out why one day but for now it's not important. Just glad that I am now aware of it and many thanks. – Paul Ruddlesdin Dec 05 '16 at 16:45
1 Answers
0
As we have worked out, it appears to be something to do with debug options in Visual Studio.
If you don't have time to troubleshoot this and you do not care about 'cleanly' exiting the application, you should be able to use the following:
Environment.Exit(0);
This will certainly break out of debug.

Asnivor
- 266
- 1
- 8