我有一个FormClosing
方法让我的程序在关闭表单之前询问用户是否要退出程序。这很好用,但我不希望在程序因系统关闭/注销而关闭时显示对话框。如何检测系统发送的kill命令,而不是用户单击表单的x? Envrionment.HasShutdownStarted
不起作用。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Need to check for system shutdown here before the next if is activated
if (MessageBox.Show(...) == DialogResult.No)
{
e.Cancel = true;
this.Activate();
}
}
答案 0 :(得分:2)
尝试检查e.CloseReason
,例如
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.WindowsShutDown)
{
if (MessageBox.Show(...) == DialogResult.No)
{
e.Cancel = true;
this.Activate();
}
}
}