我的任务需求
大约有12人使用此应用程序,但我们只想允许4人通过传统方法(Alt+F4,文件>退出,关闭)关闭应用程序。
如果使用其他任何方法(任务管理器,Windows关闭),或者允许的用户之一关闭了应用程序,我们需要执行一些清理操作(关闭一些连接通道)。
我所使用的代码来满足上述要求
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
}
问题
如果一个用户(无权关闭)试图通过传统方法关闭应用程序,然后尝试使用任务管理器关闭,则CloseReason枚举似乎不会重置自身,从而导致任务管理器弹出提示框以强制关闭,防止应用程序清理。
问题描述
这是一个 bug 吗?还是我漏掉了什么东西,可以在取消 FormClosing 事件后重置 CloseReason 呢?
e.CloseReason
是只读属性,所以您无法自行重置它。这是一个时间问题吗?如果您在从任务管理器关闭之前等待一段时间,那么可以吗? - ChrisF