Problem with WM_ENDSESSION message

强颜欢笑 提交于 2019-12-01 10:41:22

I don't think it's wrong to call PostQuitMessage in response to WM_QUERYENDSESSION.

WM_ENDSESSION is the end of the world. It's too late at that point to postpone work until a later time (calling PostQuitMessage). Do it now, or you'll never get a chance to do it. Also, consider what you are doing. As Raymond Chen once put it, "[cleaning up your app in response to WM_ENDSESSION is] like taking the time to steam-clean the rugs before you demolish the building. Wasted effort."

WM_QUERYENDSESSION grants your window a last-chance to interact with the user. You have decided on behalf of the user that your app will die and you want to gracefully exit, so this is your last opportunity to schedule it.

Updated

I don't know that will even work to PostQuitMessage in response to WM_QUERYENDSESSION. The MSDN docs state, "The WM_ENDSESSION message is sent to an application after the system processes the results of the WM_QUERYENDSESSION message."

Sent implies that the message pump doesn't get a crack at messages. Of course, even the doc authors often confuse sent and posted.

I would put

switch(msg)
{
    //...  
    case WM_ENDSESSION:  
        if(wParam) PostQuitMessage(0);  
        return 0;  
    //...  
}

putting return 0; should exit the program, if you are in the main() function

You don't need any special handling. Just call DefWindowProc instead of handling these messages.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!