问题描述
我需要检测时,Windows是关机(或重新启动),或者当用户被注销。我需要正确地关闭应用程序之前关闭应用程序。我注意到,当Windows正在关闭一天没有退出应用程序事件时引发。
I need to detect when Windows is shutdown (or restarted) or when the user is logging off. I need to properly close the application before the application is closed.I noticed that no exit application event is raised when Windows is closing day.
我阅读后的Is有没有办法在C#来检测Windows关机/注销和取消操作(询问用户后)
但我不知道我应该关闭之前执行的操作。谢谢你。
but I'm not sure of where I should perform the operations before closing.Thanks.
推荐答案
附加一个事件处理方法到<$c$c>SystemEvents.SessionEnding事件,你的处理方法会引发该事件在每次调用。处理此事件将让你的取消的未决注销或关闭,如果你想。 (虽然实际上并没有工作就像它的声音在目前的操作系统;有关详细信息,在这里看到的 MSDN文档。)
Attach an event handler method to the SystemEvents.SessionEnding
event, and your handler method will be called each time the event is raised. Handling this event will allow you to cancel the pending log off or shut down, if you wish. (Although that doesn't actually work like it sounds in current operating systems; for more information see the MSDN documentation here.)
如果您不希望取消事件,但只是适当地反应,因此,你应该处理的<$c$c>SystemEvents.SessionEnded事件代替。
If you don't want to cancel the event, but just react to it appropriately, you should handle the SystemEvents.SessionEnded
event instead.
您必须确保您的分离的事件处理程序,当应用程序被关闭,但是,因为这两个都是静态的活动。
You must make sure that you detach your event handlers when the application is closed, however, because both of these are static events.
这篇关于如何检测Windows关机或注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!