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 指示他们为什么需要取消关机.这些无声关机故障"让用户非常沮丧,他们通常需要一两分钟才能意识到关机失败,因为没有显示用户界面.

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