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.