C# - 如何检测 Windows 关闭/注销并取消该操作(询问用户后)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4617538/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:55:21  来源:igfitidea点击:

C# - How to detect a Windows shutdown/logoff and cancel that action (after asking the user)

c#.netwindowssystem-shutdown

提问by Sébastien Nussbaumer

Some explanation: for a project I'm working on I have to write a program that is running in the background, detects changes to files, and uploads the modified files to a web service to make it available to others. Quite simple synchronization if it were not for the case when a user modifies a big file and decides to shutdown its computer right after the edit.

一些解释:对于我正在处理的项目,我必须编写一个在后台运行的程序,检测对文件的更改,并将修改后的文件上传到 Web 服务以供其他人使用。如果不是用户修改大文件并决定在编辑后立即关闭其计算机的情况,则同步非常简单。

I could cancel the upload and wait for the next reboot to do the upload, but I can imagine the user downloading the file from the web to another computer the next morning and don't understanding why his changes from last night aren't there.

我可以取消上传并等待下一次重新启动以进行上传,但我可以想象用户第二天早上将文件从网络下载到另一台计算机并且不明白为什么他昨晚的更改不存在。

So my idea was to detect when the users logs off or reboots Windows, and if I'm in the middle of an upload, just asking the user "We're still synchronizing file Foo.txtthat you just changed. Are you sure you want to reboot ? You're changes won't be available to others until you restart your computer !". If the users says no, I'd need to cancel the reboot/loging off

所以我的想法是检测用户何时注销或重新启动 Windows,如果我正在上传,只需询问用户“我们仍在同步Foo.txt您刚刚更改的文件。您确定要重新启动吗? ? 除非您重新启动计算机,否则其他人将无法使用您所做的更改!”。如果用户说不,我需要取消重启/注销

Is this possible?

这可能吗?

采纳答案by Adam Houldsworth

There is a static class called SystemEventsthat exposes this behaviour:

有一个静态类称为SystemEvents公开这种行为:

http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx

http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx

However, it cannot differentiate between certain actions and doesn't pause the OS process time-out guard. I used it once, but the default time-out as configured in the registry is a little short so will likely need increasing.

但是,它无法区分某些操作,并且不会暂停操作系统进程超时保护。我用过一次,但注册表中配置的默认超时时间有点短,因此可能需要增加。

To cut a long story short, it all felt a little hackish.

长话短说,这一切都让人觉得有点骇人听闻。

回答by Tim Robinson

To add to @Adam's answer, if you need to tell the difference between logoff and shutdown/reboot, you can handle the WM_QUERYENDSESSIONmessage.

添加到@Adam的答案中,如果您需要区分注销和关机/重启之间的区别,您可以处理WM_QUERYENDSESSION消息。

"Shutdown Changes for Windows Vista" is a useful article for understanding the shutdown timeout.

Windows Vista 关机更改”是一篇了解关机超时的有用文章。

回答by Hans Passant

Trying to block a shutdown is a lossy proposition these days, it's no longer possible to do so in Vista and up. A prompt isn't readable nor reachable. Using a service is highly indicated here, lets you survive a user log-off. And a reboot, your service will start running again automatically, letting you complete the job.

如今,试图阻止关机是一个有损的提议,在 Vista 及更高版本中不再可能这样做。提示不可读也不可达。此处高度指示使用服务,可让您在用户注销后继续存在。重新启动后,您的服务将再次自动开始运行,让您完成工作。