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

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

问题描述

我想要我的应用程序可以防止Windows关闭。我知道有一个系统命令来做到这一点。但不适合我的程序。
i使用此代码取消Windows关闭:

  private void Form1_FormClosing(object sender,FormClosingEventArgs e) 
{
if(e.CloseReason.Equals(CloseReason.WindowsShutDown))
{
MessageBox.Show(取消Windows关闭);
string cmd =shutdown / a;
Process.Start(cmd); //执行系统命令。
}
}

并使用此代码,但不起作用: (:

  public Form1()
{
InitializeComponent();

SystemEvents.SessionEnding + = SessionEndingEvtHandler;
}

private void SessionEndingEvtHandler(object sender,SessionEndingEventArgs e)
{
MessageBox.Show(取消Windows关闭) ;
string cmd =shutdown / a;
Process.Start(cmd); //执行系统命令
}
pre>

我将不胜感激,如果有人解释我如何在取消Windows关闭
谢谢

解决方案

这是非常不利的,微软尽可能的努力,如果用户想关闭,那么用户的责任,而不是应用程序。根据Microsoft文章应用程序n Windows Vista中的关机更改


不再允许静默关机取消



在Windows XP中,应用程序可以否决WM_QUERYENDSESSION
,而不显示任何用户界面,表示为什么需要取消关机。
这些静默关机失败对用户来说非常令人沮丧,
经常需要一两分钟才能意识到关闭失败,因为
没有UI被显示。



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


...还...


应用程序不应阻止关闭



如果你只读一件事,阅读这个话题,它应该是
这一个。如果
您的应用程序不会阻止关闭,您将向您的用户展示最佳体验。当用户启动
关机时,在绝大多数情况下,他们有强烈的愿望
看到关机成功;例如,他们可能会匆忙离开办公室
在周末。应用程序应该通过
来遵守这一愿望,如果可能的话,不要阻止关闭。


如果你真的需要在关机期间进行intevene ,您应该注册一个新的API:


使用新关机原因API



新的关闭原因API由三个功能组成:

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

同样,关闭Windows Vista应用程序的最佳做法是
,他们不应该阻止关掉。但是,如果您的应用程序
必须阻止关闭,Microsoft建议您使用此API。


但是在一天,所有这一切都将提供给用户的用户界面,以表示应用程序正在阻止关闭并询问用户是否要继续并强制关闭。如果他们回答是,你不能阻止这个,没有办法阻止UI。



阅读我链接到的MSDN文章 - 它解释了来自Vista的模型。最终,这种范式是给用户提供控制,并防止应用程序覆盖用户需求。


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.
    }
}

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.  
}

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

解决方案

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:

Silent shutdown cancellations will no longer be allowed

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 will eliminate this possibility by displaying UI even if an application vetoes WM_QUERYENDSESSION.

...also...

Applications should not block shutdown

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.

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

Using the New Shutdown Reason 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);

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.

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.

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天全站免登陆