我的计算机有一个备用电源,它与计算机和墙壁串联。当我从墙上拔下电源线时,备用电源会在 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;
}
}
问题是它没有用。我认为闭幕式的活动从来没有被调用过。任何想法将不胜感激。谢谢你。