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

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

问题描述

我希望我的应用程序可以防止 Windows 关闭.我知道有一个系统命令可以做到这一点.但不要为我的程序工作.我使用此代码取消"关闭窗口:

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

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

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

推荐答案

这是非常不明智的建议,Microsoft 尽可能地难以做到这一点.如果用户想要关闭,那么这是用户的责任,而不是应用程序.根据 Microsoft 文章 Windows Vista 中的应用程序关闭更改:

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.

...还有...

应用程序不应阻止关机

如果您在阅读本主题时只需要做一件事情,那应该是这个.如果满足以下条件,您将为用户提供最佳体验您的应用程序不会阻止关机.当用户发起关闭,在绝大多数情况下,他们有强烈的愿望看到关机成功;他们可能急于离开办公室以周末为例.应用程序应该尊重这种愿望尽可能不阻止关机.

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.

如果您确实需要在关机期间进行干预,您应该注册一个新的 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.

但归根结底,所有这一切都会向用户显示一个用户界面,告诉用户应用程序正在阻止关闭,并询问用户是否要继续并强制关闭.如果他们回答是,则您无法阻止此操作,也无法阻止 UI.

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 开始的模型.归根结底,范式是赋予用户控制权,并防止应用程序凌驾于用户需求之上.

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