【发布时间】:2018-04-23 14:22:58
【问题描述】:
在我的应用程序中,我有主窗口,用户可以从中打开另一个 WPF 窗口。 当用户关闭主窗口时,我想退出应用程序,所以我写了这段代码:
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);
}
}
但第二个窗口仍然出现并运行,因为它有一个等待信号的线程。
为了防止它,我添加了结束等待Window_Closing 事件信号的线程的代码。现在,如果用户关闭第二个窗口,它可以工作(线程停止等待,然后窗口关闭),但是如果这个窗口需要从主窗口关闭,它就不起作用,窗口继续等待。
可能Application.Current.Shutdown(); 不会导致Window.Closing 事件?
如果不是,我该如何停止在第二个窗口中的等待,或者换句话说,我知道窗口将要关闭?
谢谢。
【问题讨论】: