0

I have a WPF application where I have two windows. First window is just a login window for users. So whenever the user successfully logs in, 2nd window appears. To normally shut down the complete application, user will press the red cross button and I have included this event in my code as below:

/* Shut Down the application when user clicks red cross button*/
    void Window_Closing(object sender, CancelEventArgs e)
    {
        System.Windows.Application curApp = System.Windows.Application.Current;
        curApp.Shutdown();

    }

So the above code completly shut down the application. But let say, if user do not want to login and simply wants to close the application while he is in the 1st window (login window). In that case, application should also shut down. So I included the above same code for the red cross button of the 1st login window. But that created another problem, because the next time any user logs in, I am closing the 1st window and calling the 2nd window. In this scenairo, application completely shuts down because of window closing event of 1st window.

How can I handle this condition.?

S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • `Closing` is called for either programmatic or user closing of the window (and some others reasons). You have to distinguish between them. E.g. if you are closing (by calling `window.Close()`) set some flag (`bool` field) and check it inside `Closing` event handler. – Sinatr Feb 23 '17 at 11:29
  • @Sinatr Thanks for pointing me to the correct question. :). That solved my issue – S Andrew Feb 23 '17 at 11:33

1 Answers1

0

Wouldn't it be sufficient if you let the framework shutdown the application implicitly? The application is usually shutdown, if the last window is closed. I think you shouldn't need any explicit shutdown code. If the login window is closed, the application should shut down (because it was the only window). After a successful login, the second window is opened and the login window is closed. If the user then closes the second window, the application should shut down, because it was the last window again.

Take a look at the different ShutdownModes:

https://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode(v=vs.110).aspx

doerig
  • 1,857
  • 1
  • 18
  • 26