C# Windows 应用程序阻止 Windows 关闭/注销
我编写了一个 C# Windows 窗体应用程序,而不是一个具有无限循环运行的后台线程的服务(它仅在用户登录并具有图形用户界面时使用)。
然而,当我尝试关闭 Windows (7) 时,它告诉我该程序正在阻止它关闭或注销,并询问我是否要强制关闭。
现在,我的程序是否有可能意识到(获取处理程序)Windows 试图退出它或注销?
所以,我需要的是让应用程序在Windows尝试退出时实现。
提前致谢。
编辑:感谢您的好建议!如果表单关闭事件有 CANCEL 事件处理程序,是否可以以任何方式使用该想法?
I have written a C# Windows Forms application, not a service (it is only used when the user is logged in and has a graphical user interface) that has a background thread running in an infinite loop.
When I try shutting down Windows (7) however, it tells me the program is preventing it from shutting down or logging off and asks me whether I want to force a shutdown.
Now, is there any possibility for my program to become aware (get a handler) of Windows trying to quit it or to log off?
So, what I need is to make the application realize when Windows tries to quit.
Thanks in advance.
EDIT: Thanks for the great advice! Is it in any way possible to use the idea with the form closing event if it has a CANCEL event handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将该线程称为“后台线程”,但它是否具有
IsBackground = true;
?系统只会停止这样做的线程。
You call that thread a "background thread" but does it have
IsBackground = true;
?The system will only stop a thread that does.
我认为 Capture console exit C# 也应该在您的场景中可用。
除此之外,也许将线程设置为 后台线程?
I think Capture console exit C# should also be usable in your scenario.
Apart from that, maybe it is sufficient to set up your thread as background thread?
看一下 Microsoft.Win32.SystemEvents.SessionEnding 事件。
Take a look at the Microsoft.Win32.SystemEvents.SessionEnding event.