是否有可能检测到Application.Exit被调用或是否有win32函数来了解窗口何时关闭程序,因为关闭或任何事情。没有表格。
答案 0 :(得分:2)
有关WM_QUERYENDSESSION和WM_ENDSESSION消息的MSDN文档,请参阅; WM_ENDSESSION消息的参数将告诉您应用程序是正常退出还是Windows正在关闭。您可以通过覆盖表单中的WndProc方法来处理这些消息,如:
public partial class MainForm : Form
{
private const int WM_ENDSESSION = 0x0016;
private const uint ENDSESSION_CLOSEAPP = 0x1;
private const uint ENDSESSION_CRITICAL = 0x40000000;
private const uint ENDSESSION_LOGOFF = 0x80000000;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_ENDSESSION)
{
var sessionEnding = m.WParam.ToInt32() != 0;
if ((m.LParam.ToInt64() & ENDSESSION_CLOSEAPP) == ENDSESSION_CLOSEAPP)
{
// App closing
}
if ((m.LParam.ToInt64() & ENDSESSION_CRITICAL) == ENDSESSION_CRITICAL)
{
// Critical error
}
if ((m.LParam.ToInt64() & ENDSESSION_LOGOFF) == ENDSESSION_LOGOFF)
{
// User logging off
}
m.Result = IntPtr.Zero;
}
else
{
base.WndProc(ref m);
}
}
}
答案 1 :(得分:1)
这个怎么样?
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
然后
/// <summary>
/// Method called when the process is exiting.
/// </summary>
private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
// do something
}