问题描述
我正在开发的C#.NET 4.0的Windows窗体应用程序。我想捕获窗口注销事件。
下面是code:
公共Form1中()
{
的InitializeComponent();
SystemEvents.SessionEnding + =(S,E)=>
{
如果(e.Reason == SessionEndReasons.Logoff)
{
的MessageBox.show(注销);
}
否则,如果(e.Reason == SessionEndReasons.SystemShutdown)
{
的MessageBox.show(关闭);
}
};
}
为什么心不是我的sessionEnding发射?
- 这取决于它被设置在GPEDIT.MSC配置。
打开GPEDIT.MSC,浏览到计算机配置 - >管理模板>系统>关机选项,然后选择关闭自动终止,阻止或取消关机的应用程序。由于我的笔记本电脑配置让它自动关机,所以不会触发会话终止
-
也许你可以将你的code以上到其窗口入口点(主)。
-
也许你可以重写窗口的消息。你可以看到它在MSDN库文档。的
-
关机消息泵已被重新路由通过其他软件,而不是重新路由到您的应用程序
I am developing an windows forms application in c# .net 4.0.I want to capture windows logoff event.
Here is the code:
public Form1()
{
InitializeComponent();
SystemEvents.SessionEnding += (s, e) =>
{
if (e.Reason == SessionEndReasons.Logoff)
{
MessageBox.Show("LogOff");
}
else if (e.Reason == SessionEndReasons.SystemShutdown)
{
MessageBox.Show("ShutDown");
}
};
}
Why isnt my sessionEnding firing?
- It depends on the configuration that is set on gpedit.msc.
Open gpedit.msc, navigate to Computer Configuration > Administrative Templates > System > Shutdown Options and choose Turn off automatic termination of applications that block or cancel shutdown. Since mine laptop configure make it automatic shutdown, so it will never fire session ending
Perhaps you can move your code above into entry point of its windows (in the main).
Perhaps you can override windows message. You can see it in MSDN library documentation.http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx
Shutdown message pump has been re route by other software and not re route to your apps
这篇关于SystemEvents.SessionEnding没有发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!