Problem with WM_ENDSESSION message

前端 未结 3 615
甜味超标
甜味超标 2021-01-15 05:08

I have a problem with WM_ENDSESSION message. Namely I would like to exit from the main loop of the application (WindowProc) when the WM_ENDSESSION

相关标签:
3条回答
  • 2021-01-15 05:40

    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.

    0 讨论(0)
  • 2021-01-15 05:50

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

    0 讨论(0)
  • 2021-01-15 05:54

    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

    0 讨论(0)
提交回复
热议问题