0

In my app I have the main window, the user can open another WPF windows from it. When the user closes the main window I want to exit from the application, so I Wrote this code:

  private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (onClosingAppFlag == true)
                    return;

                string s = "       Exit the application ?" + Environment.NewLine +
                            "(Exit process duration is about 5 seconds)" + Environment.NewLine +
                            "       Are you sure? ";
                MessageBoxResult dres = MessageBox.Show(this, s, "Exit from RTS",
                    MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (dres == MessageBoxResult.No)
                {
                    e.Cancel = true;
                    return;
                }

                onClosingAppFlag = true;
                App.Current.Shutdown();
                OnClosingApp();

            }
            catch (Exception ex)
            {
                string line = "Error, Exeption occuredd at MainWindow.Window_Closing() : " + Environment.NewLine + ex.Message;
                AppWinServices.Singleton.LogFileWrite(line);
            }
        }

But the second window still appears and running because it has a thread that waits for signals.

To prevent it I added code that ends the thread that waiting for signals on Window_Closing event. Now if the user closes the second window it works (The thread stop to wait and then the window becomes closed), but if this window needs to be closed from the main window it doesn't work and the window continue to wait.

It can be that Application.Current.Shutdown(); doesn't cause to Window.Closing event? and if not how can I stop the waiting in the second window or in other words where I know the window is going to be closed?

Thanks.

someone
  • 31
  • 1
  • 6

1 Answers1

0

Call Environment.Exit(0) or set the ShutdownMode property to OnMainWindowClose in your App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    ShutdownMode = ShutdownMode.OnMainWindowClose;
}

what's difference between Environment.Exit() and Application.Shutdown()?

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I can't use ShutdownMode because I need to do extra things before closing the app :( – someone Apr 23 '18 at 14:45
  • Do them before you call the method then...? Or go with my second suggestion. – mm8 Apr 23 '18 at 14:46
  • I can't. Is it not too brutal ,as explained in the link? – someone Apr 23 '18 at 14:48
  • You can't what? You asked on how to close the application immediately without waiting, didn't you? Or what is your question? – mm8 Apr 23 '18 at 14:49
  • I can't do extra things before closing the app if I use ShutdownMode. and my question was how can I know in the window that it needs to be closed. – someone Apr 23 '18 at 14:58
  • How you can know what where...? You should really read this and modify your question accordingly: https://stackoverflow.com/help/mcve – mm8 Apr 23 '18 at 15:02