【问题标题】:C# How to detect kill event sender [duplicate]C#如何检测终止事件发送者[重复]
【发布时间】:2015-02-27 13:42:07
【问题描述】:

我的程序有一个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();
    }
}

【问题讨论】:

标签: c# detect shutdown


【解决方案1】:

尝试检查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();
        }
    }   
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 2010-11-26
    • 2016-01-14
    相关资源
    最近更新 更多