相关疑难解决方法(0)

FormClosingEventArgs.CloseReason中的错误?

我要求的要求

大约有12人正在使用此应用程序,但我们只想允许4通过传统方法关闭应用程序(Alt + F4,文件>退出,关闭)

如果使用任何其他方法(TaskManager,WindowsShutdown)或其中一个允许的用户关闭应用程序,我们需要执行一些清理(关闭一些连接通道)

我曾用过的守则满足上述要求

private void formClosing(object sender, FormClosingEventArgs e)
{
    // If a user is allowed to close the application, an empty file (filename)
    // will be in the root directory of the application.
    if(e.CloseReason == CloseReason.UserClosing && !File.Exists("filename"))
    {
        e.Cancel = true;
        return;
    }

    // Cleanup
}
Run Code Online (Sandbox Code Playgroud)

问题

如果用户(不允许关闭)尝试通过传统方法关闭应用程序,则尝试使用任务管理器关闭CloseReason枚举似乎不会自行重置,从而导致任务管理器弹出提示强制关闭,从而阻止清理申请.

问题

这是一个错误,或者我错过了什么,会在FormClosing事件被取消后重置CloseReason.

c# winforms

6
推荐指数
1
解决办法
2782
查看次数

标签 统计

c# ×1

winforms ×1