I need it for their own exit button. Tell me please? I try this: this.Close(); //or Exit dont work(
3 Answers
You can use the CoreApplication
class. It provides a static exit method:
public void CloseApp()
{
CoreApplication.Exit();
}
However, the documentation states the following:
Note Do not use this method to shut down an app outside of testing or debugging scenarios.
Sadly, the reason behind that is left unkown.
Further more, you can use the old-fashioned Application.Exit
method (non-static):
public void CloseApp()
{
Application.Current.Exit();
}
Here you should also take a look in the remarks:
Use this method to provide UI that enables users to exit your app. Normally, however, you should not provide this UI because the system automatically manages app lifetime and terminates suspended apps as needed to free resources.
tl;dr:
Both Exit
methods will terminate the app, rather than suspending it. You should ask yourself if this really is the action you want to do.

- 5,851
- 2
- 35
- 61
-
2The reason is it's against the UX guide lines to close the app programmatically – Nuwan Karunarathna Dec 05 '16 at 05:22
-
Application.Current.Exit() can cause a catastophic failure. – Gavin Williams Aug 26 '20 at 06:02
-
1The UX guidelines are inadequate if they don't support closing an app programmatically. That particular clause would single handedly reject the design of every game running on Windows. – Gavin Williams Aug 26 '20 at 06:07
-
@GavinWilliams could you clarify what you mean with "catastrophic"? Could you name a few examples or provide a few catchwords to search for? Couldn't find anything on this. – Chris Tophski Feb 08 '23 at 17:55
This is the supported way of exiting a UWP app:
Application.Current.Exit();
It is however relatively rare that you should use it. Consider carefully the UI experience related to the scenario where you would use this method. For example it may be justified to programmatically exit an application if some account has expired or security permissions managed remotely were revoked. It is rare that you have your own "Exit" button sitting in the middle of your screen without contravening Windows guidelines.

- 1,274
- 9
- 17
-
12"It is rare that you have your own "Exit" button sitting in the middle of your screen ..." - This is actually very common, as almost without exception, every game ever made has a quit button! – Gavin Williams Mar 17 '17 at 12:44
-
4
-
5(1) The method doesn't work. It seems to destroy the main window, but the debugger tells me the program is still running. (2) What else are you going to do with the application if the user doesn't have login credentials? You're going to let the screen sit there forever? – Quark Soup Jun 02 '18 at 22:04
If you want to suspend the app instead of terminating try to use ApplicationView.TryConsolidateAsync()
. For example, if app implements only one ApplicationView
try calling ApplicationView.GetForCurrentView().TryConsolidateAsync()
to close the app.
The obvious benefit of this method is app is closed just as you will do by pressing close button in titlebar, the closing is graceful, animation is the same and app is suspended instead of abruptly exiting.
Also, when you launch your app again after closing by this method, app starts in the same position and size as you closed it before while using Application.Current.Exit()
and CoreApplication.Exit()
doesn't start the app in the same position and size.

- 2,148
- 12
- 30
-
3+1. Didn't know about this method. In the documentation it says "This method is a programmatic equivalent to a user initiating a close gesture for the app view.", so it seems to be better than the other methods. – Xam Aug 22 '20 at 16:36
-
1
-
This works really well. I'm wondering why the `SystemNavigationManagerPreview.GetForCurrentView().CloseRequested` event is not being invoked. – pinki Aug 25 '20 at 08:25
-
It might not invoke if the window is main window, if you are manually closing why not add your function before that?? Can you describe why you need to subscribe to this event?? – Soumya Mahunt Aug 25 '20 at 10:40
-
ApplicationView.GetForCurrentView().TryConsolidateAsync() can throw an exception - element not found. – Gavin Williams Aug 26 '20 at 06:05
-
@Gavin Williams I couldn't find the documentation about "Element not found" exception, but it seems that you are trying to call the method before even `ApplicationView` is created hence the error. Can you describe when you are getting this exception?? – Soumya Mahunt Aug 29 '20 at 14:06