本文介绍了我应该处理 WM_ENDSESSION、WM_QUERYENDSESSION,还是两者都不处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果系统试图关闭,应用程序可以通过覆盖 OnQueryEndSession() 并返回 FALSE 来阻止此关闭.当然,这意味着 WM_ENDSESSION 是有关关机的唯一确定性消息.

If a system is trying to shut down, an app can block this shutdown by overriding OnQueryEndSession() and returning FALSE. Surely that means WM_ENDSESSION is the only definitive message to respond to regarding shutdown.

另一方面,这个问题的最佳答案引用不少于Raymond Chen 说回应 WM_ENDSESSION 基本上没有意义.所以这很令人困惑.

On the other hand, the top answer to this question quotes no less than Raymond Chen as saying that responding to WM_ENDSESSION is essentially pointless. So this is confusing.

是否有某种最佳实践"原则可用于决定应响应哪些消息(如果有)以执行哪些类型的应用程序关闭工作?

Is there some kind of "best practice" principles to apply in deciding which of these messages (if any) one should respond to for doing what kinds of application shutdown work?

特别是,如果两个消息都没有处理,关闭过程是否会导致应用程序关闭,就像用户手动关闭应用程序一样(例如,点击红色 X 关闭按钮)?

In particular, if neither message is handled, will a shutdown process cause an application to be closed as if the user had closed the application manually (e.g. click on red X close button)?

推荐答案

这篇来自 Microsoft 的文章对Vista 之前和之后的会话结束最佳实践进行了非常全面的讨论.这篇文章非常清楚地表明,人们应该假设,如果收到 WM_QUERYENDSESSION,那么在某个时刻会发生关机.

This article from Microsoft gives a very comprehensive discussion of end-of-session best practice both pre- and post-Vista. The article makes it quite clear that one should assume that if one receives a WM_QUERYENDSESSION then shutdown will occur at some point.

一旦所有应用程序都响应了 WM_ENDSESSION 消息,或者在收到 WM_ENDSESSION 消息后 5 秒内被迫终止,Windows 可能随时关闭.这可能会限制对 WM_ENDSESSION 的响应.

As soon as all applications have responded to the WM_ENDSESSION message, or been forced to terminate within 5 seconds of receiving the WM_ENDSESSION message, Windows may shut down at any time. This may limit what can be done in response to WM_ENDSESSION.

如果应用程序需要更多时间来清理自身:

If an application requires more time to clean itself up:

如果您的应用程序响应 WM_ENDSESSION 可能需要超过 5 秒来完成其关闭处理,则应在其 WM_QUERYENDSESSION 处理程序中调用 ShutdownBlockReasonCreate(),并及时响应 WM_QUERYENDSESSION 的 TRUE 以免阻止关闭.然后它应该在其 WM_ENDSESSION 处理程序中执行所有关闭处理.

Windows 显然不会向您的应用程序发送任何额外的消息以允许它正常"退出(例如 WM_CLOSE).相反,它会简单地调用 TerminateProcess.如果你想要一个优雅的关闭,你必须在上述限制内自己构建它.

Windows will apparently not send any additional messages to your application to allow it to exit "gracefully" (e.g. WM_CLOSE). Rather, it will simply call TerminateProcess. If you want a graceful close, you have to build it yourself within the above constraints.

这篇关于我应该处理 WM_ENDSESSION、WM_QUERYENDSESSION,还是两者都不处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:15