How to exit a WPF application


  • Is there a way to force a WPF application to exit,.. something like application.Exit() in windows forms?

    If not, how can I, for example force an application to exit on a button click?

    Thanks in advance,
    Marko Vuksanovic.

    Wednesday, September 13, 2006 9:28 PM


All replies

  • Have you tried going through all the Windows in the Application.Windows collection and calling Close() on them?

    You could always do a process kill
    Wednesday, September 13, 2006 10:36 PM
  • Application has a ShutDown method.

    I discuss the application object and more on my blog.


    Wednesday, September 13, 2006 11:26 PM
  • Well it seems like Application.Current.Shutdown(); should work fine.... but there seems to be a problem.

    If I open window2 from window1 (when it is not yet displayed, that is, in window1 constructor) and then execute Application.Current.Shutdown(); when a button is clicked the application crashes.

    Any idea what might be wrong?

    P.S. Below is the Window1 constructor:

    public Window1()



    // Insert code required on object creation below this point.

    while (isAuthenticated == false)





    if (isAuthenticated == false)


    //show authentication Authentication PopUp Window

    MeiRouInterface.Window2 popUp = new MeiRouInterface.Window2();





    Thursday, September 14, 2006 9:51 AM
  • When you call Window.ShowDialog(), WPF will actually pump a new nested message loop, the problem is that during the initialization of Window1, there is some layout taking place, the problem here is that WPF will disable nested message loop when doing layout, that's why your application crashes when you call popUp.ShowDialog(), actually Nick Krammer has a blog article dedicated to nested message loop.

    Nested message loops are evil (if you're a platform)


    Thursday, September 14, 2006 1:35 PM
  • so if that is the issue, perhaps your first window should listen to its own Loaded event and then show the dialog.
    Thursday, September 14, 2006 2:36 PM
  • If I show the dialog when the loaded event is fired, won't there already be window1 shown?
    Friday, September 15, 2006 8:00 AM