在 C# 中拦截 Windows Vista 关闭事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/148733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intercept Windows Vista shutdown event in C#
提问by HAdes
I want to be able to intercept the shutdown event in C# for Windows Vista. Due to the advanced security features with Vista, any applications that are running after the shutdown command is called are halted and displayed in a list, prompting the user to do something with them.
我希望能够在 Windows Vista 的 C# 中拦截关闭事件。由于 Vista 的高级安全功能,在调用关闭命令后运行的任何应用程序都将停止并显示在列表中,提示用户对它们进行处理。
Does anybody know how to overcome this and what events I need to be using in Vista.
有谁知道如何克服这个问题以及我需要在 Vista 中使用哪些事件。
Thanks.
谢谢。
回答by Ken Wootton
You can use WPF's application object and subscribe to its SessionEnding event. You can then look at the SessionEndingCancelEventArgs.ReasonSessionEndingenumeration to determine exactly why the session is ending (LogOff or Shutdown).
您可以使用 WPF 的应用程序对象并订阅其 SessionEnding 事件。然后,您可以查看SessionEndingCancelEventArgs.ReasonSessionEnding枚举以确定会话结束的确切原因(注销或关闭)。
回答by Philip Rieck
What you may want to look at is here - Application Shutdown Changes in Windows Vista. Basically, for what you want, it all revolves around WM_QUERYENDSESSION.
您可能想要查看的内容在这里 - Windows Vista 中的应用程序关闭更改。基本上,对于你想要的,这一切都围绕着 WM_QUERYENDSESSION。
Note that this is exposed in the .net framework - instead you will need to use native functions (p/invoke) and hook the wndproc in your code to respond to the windows message.
请注意,这是在 .net 框架中公开的 - 相反,您需要使用本机函数(p/invoke)并在代码中挂钩 wndproc 以响应 Windows 消息。
For an example (showing a reason to not shutdown), you can see Windows Vista - ShutdownBlockReasonCreate in C#.
例如(显示不关闭的原因),您可以在 C# 中查看Windows Vista - ShutdownBlockReasonCreate。
回答by mattlant
Use the event
使用事件
Application.SessionEnding for WPF.
WPF 的 Application.SessionEnding。
回答by csgero
The SessionEnding / SessionEnded events on Microsoft.Win32.SystemEvents might be what you are looking for.
Microsoft.Win32.SystemEvents 上的 SessionEnding / SessionEnded 事件可能正是您要找的。
回答by Eduardo Campa?ó
System.Environment.HasShutdownStarted