user1404173
user1404173

Reputation: 247

Infinite windows message loop

I have this message loop in my program:

while (true) {
    if (PeekMessage(&msg, window, 0, 0, PM_REMOVE)) {
        if (msg.message == WM_QUIT) {
            MessageBox(NULL, L"Quit", L"", 0);
            break;
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    } else {
        Render();
    }
}

This loop never ends. It never displays the message box even though main window disappears. Here is the WndProc code:

switch (msg) {

    case WM_CLOSE :
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY :
        PostQuitMessage(0);
        break;

    default :
        return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
}

return 0;

Could someone please help me? I am literally pulling my hairs out.

Upvotes: 0

Views: 2868

Answers (1)

jamesdlin
jamesdlin

Reputation: 89926

You're calling PeekMessage(&msg, window, ...). If window isn't NULL, you'll never get WM_QUIT, because WM_QUIT is not associated with a window.

Instead, just call PeekMessage/GetMessage with a NULL HWND. DispatchMessage will send it to the right WndProc as necessary. (In general, making GetMessage/PeekMessage filter by HWND is a bad idea.)

Upvotes: 9

Related Questions