gpt4 book ai didi

c# - FormClosing 关闭事件不写入文件

转载 作者:太空宇宙 更新时间:2023-11-03 21:59:47 25 4
gpt4 key购买 nike

我的电脑有一个备用电源,它与电脑和墙壁串联连接。当我从墙上拔下电源线时,备用电源会在 2-5 分钟后关闭计算机。正是在这段时间里,我想使用以下代码将数据写入文件:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
{
writeContents("Interrupted");
sendMessage("PWR - The Spring Test Machine has stopped");
return;
}

if (e.CloseReason.Equals(CloseReason.UserClosing))
{
if (MessageBox.Show("You are closing this application.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes)
{
writeContents("Interrupted");
return;
}
else
e.Cancel = true;
}
}

问题是它不起作用。我认为闭幕式从未被召集过。任何想法将不胜感激。谢谢。

最佳答案

从这里http://www.daniweb.com/software-development/csharp/threads/253249/application-exit-does-not-trigger-the-formclosing

when you use taskmanager to "kill" the appliction, it doesn't fire any events, It simply stops the execution, This is how you close an application that has frozen. if you waited for it to handle any events, then it would still be frozen.

as for when you restart the computer or shutdown, the event will be called only if there is enough time, they system tells all applications it is shutting down and only gives them a short amount of time to handle business before it kills them. Windows 7 will show a dialog telling you which applications are still busy and ask you if you want to kill them and shutdown, or cancel. But as for XP, it just kills them after X amount of seconds.

这几乎就是我对 FormClosing 行为的想象……Application.ApplicationExit Event 会是一个更好的监听事件吗?

关于c# - FormClosing 关闭事件不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10686323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com