What's the logical difference between PostQuitMessage() and DestroyWindow()?

10,571

Solution 1

DestroyWindow destroys the window (surprise) and posts a WM_DESTROY (you'll also get a WM_NCDESTROY) to the message queue. This is the default behaviour of WM_CLOSE. However, just because a window was destroyed does not mean the message loop should end. This can be the case for having a specific window that ends the application when closed and others that do nothing to the application when closed (e.g., an options page).

PostQuitMessage posts a WM_QUIT to the message queue, often causing the message loop to end. For example, GetMessage will return 0 when it pulls a WM_QUIT out. This would usually be called in the WM_DESTROY handler for your main window. This is not default behaviour; you have to do it yourself.

Solution 2

Neither snippet is correct. The first one will do what the default window procedure already does when it processes the WM_CLOSE message so is superfluous. But doesn't otherwise make the application quit, it should keep running and you'd normally have to force the debugger to stop with Debug + Stop Debugging. If you run it without a debugger then you'll leave the process running but without a window so you can't tell it is still running. Use Taskmgr.exe, Processes tab to see those zombie processes.

The second snippet will terminate the app but will not clean up properly since you don't pass the WM_CLOSE message to the default window procedure. The window doesn't get destroyed. Albeit that the operating system will clean up for you so it does all come to a good end, just without any bonus points for elegance.

The proper way to do it is to quit when your main window is destroyed. You'll know about it from the WM_DESTROY notification that's sent when that happens:

case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
Share:
10,571
rails_has_elegance
Author by

rails_has_elegance

Updated on September 15, 2022

Comments

  • rails_has_elegance
    rails_has_elegance over 1 year

    In my demo kinda app

    case WM_CLOSE:
        DestroyWindow(hndl);
        return 0;
    

    and

    case WM_CLOSE:
        PostQuitMessage(0);
        return 0;
    

    do the same. What's different behind the curtains when calling each? Is DestroyWindow more direct, where as PostQuitMessage has to go through the getmessage loop returning false?

    • josesuero
      josesuero almost 11 years
      Consider what might happen in an application which has multiple windows. DestroyWindow destroys a window. PostQuitMessage... posts a quit message: that is, it indicates that the entire application wants to quit.
    • josesuero
      josesuero almost 11 years
      When you're new to the Win32 API, it's not immediately obvious where the line is drawn between "my window" and "my application". The message pump API really isn't very clear or helpful there. But it is important to realize that a distinction exists between the two. Do you want to close the window, or exit the application?