C#取消Windows关机 [英] C# Cancel Windows Shutdown

查看:265
本文介绍了C#取消Windows关机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的应用程序可以关闭Windows prevents。我知道,有一个系统命令来做到这一点。但不要对我的程序工作。
我用这个code为取消关闭窗口:

i want to my application can prevents from windows shut down. i know that there is a system command to do that. but don't work for my program. i use this code for "cancel" the windows shut down:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
    {
        MessageBox.Show("Cancelling Windows shutdown");
        string cmd = "shutdown /a";
        Process.Start(cmd);// for executing system command.
    }
}

,也可以使用此code,但不工作:(:

and also use this code, but does not work :( :

public Form1()
{
    InitializeComponent();

    SystemEvents.SessionEnding += SessionEndingEvtHandler;
}

private void SessionEndingEvtHandler(object sender, SessionEndingEventArgs e)
{
    MessageBox.Show("Cancelling Windows shutdown");
    string cmd = "shutdown /a";
    Process.Start(cmd);// for executing system command.  
}

如果谁能解释我如何能取消Windows关机我将不胜感激。
谢谢

i would be grateful if anyone explain me how can in "cancel" windows shutdown. thanks

推荐答案

这是强烈不明智和微软使其坚硬如能做到这一点。如果用户想要关闭,那么它是用户的责任心,而不是应用程序。按照在Windows Vista Microsoft文章应用停机的变化:

This is strongly ill advised and Microsoft make it as hard as possible to do this. If a user wants to shut down, then it is the user's responsiblity, not the applications. As per the Microsoft article Application Shutdown Changes in Windows Vista:

静音关机取消将不再被允许

在Windows XP中,应用程序被允许否决WM_QUERYENDSESSION
  无显示,说明为什么他们需要取消关机任何UI。
  这些沉默的关机失败是非常令人沮丧的用户,谁
  往往需要一两分钟认识到关机失败,因为
  在显示任何UI。

In Windows XP, applications are allowed to veto WM_QUERYENDSESSION without displaying any UI indicating why they need to cancel shutdown. These "silent shutdown failures" are highly frustrating to users, who often take a minute or two to realize that shutdown has failed because no UI was displayed.

,Windows Vista将消除,即使显示的UI这种可能性
  一个应用程序否决WM_QUERYENDSESSION。

Windows Vista will eliminate this possibility by displaying UI even if an application vetoes WM_QUERYENDSESSION.

...也... ...

...also...

应用程序应该不会阻止停机时

如果你只需要一件事从阅读这个话题了,它应该是
  这个。您将presenting给用户,如果最好的体验
  您的应用程序不会阻止关机。当用户启动
  关机,在绝大多数情况下,他们有强烈的愿望
  看到关机成功;他们可能是有急事要离开办公室
  周末,例如。应用程序应该通过尊重这一愿望
  如果在所有可能不会阻止关机。

If you take only one thing away from reading this topic, it should be this one. You will be presenting the best experience to your users if your application does not block shutdown. When users initiate shutdown, in the vast majority of cases, they have a strong desire to see shutdown succeed; they may be in a hurry to leave the office for the weekend, for example. Applications should respect this desire by not blocking shutdown if at all possible.

如果您确实需要关闭时intevene,还有,你应该注册一个新的API:

If you really do need to intevene during shutdown, there is a new API which you should register with:

使用新的停机原因API

新的关机原因的API包括三个功能:

The new shutdown reason API consists of three functions:

BOOL ShutdownBlockReasonCreate(HWND hWnd, LPCWSTR pwszReason);
BOOL ShutdownBlockReasonDestroy(HWND hWnd);
BOOL ShutdownBlockReasonQuery(HWND hWnd, LPWSTR pwszBuff, DWORD *pcchBuff);


  
  

此外,Windows Vista的应用程序在关机时最好的做法是
  他们不应该阻止关机。但是,如果你的应用程序
  必须阻止关机,Microsoft建议您使用此API。

Again, the best practice for Windows Vista applications at shutdown is that they should never block shutdown. However, if your application must block shutdown, Microsoft recommends that you use this API.

但在一天结束的时候,这一切都将做的是present的用户界面给用户说,应用程序是preventing关机并询问用户是否要继续,并强制关闭。如果他们回答是,你不能阻止这一点,并没有办法阻止用户界面。

But at the end of the day, all this will do is present a user interface to the user to say that application is preventing shutdown and asking the user if they want to continue and force shutdown anyway. If they answer yes, you can't block this, and there is no way to block the UI.

阅读MSDN文章我已经联系到 - 它从Vista explians模型开始。最终,模式是让用户控制的一个,和preventing应用覆盖用户需求。

Read the MSDN article I've linked to - it explians the model from Vista onwards. Ultimately, the paradigm is one of giving users control, and preventing applications overriding user demands.

这篇关于C#取消Windows关机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆