none
How to exit a WPF application

    Question

  • 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

Answers

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.
        Application.Current.Shutdown();

    I discuss the application object and more on my blog.

     

    Wednesday, September 13, 2006 11:26 PM
    Moderator
  • 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()

    {

    this.InitializeComponent();

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

    while (isAuthenticated == false)

    {

    InitializeCheckIfApplicationIsAuthenticatedWorkflow();

    instance.Start();

    waitHandle.WaitOne();

    if (isAuthenticated == false)

    {

    //show authentication Authentication PopUp Window

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

    popUp.ShowDialog();

    }

    }

    }

    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)

    Sheva

    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
    Moderator
  • 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