LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT); //Adds WM_QUIT to the message queue
break; //indicating that it is to gquit in the future
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
This is a code for creating an empty window. My problem is, when I click the 'x' button to exit, the window closes but the process is not dying so i have to manually kill the process in the windows task manager.
Can anyone tell me what is wrong with this code? Thanks!
Adding to what rip-off said, if there are two messages in the queue, and in this order: WM_QUIT, SOME_RANDOM_THING, it won't work as you expect. And because you use an infinite while loop instead of one working off a modifiable variable, it seems the would-be obvious solution was obfuscated. Such things happen when you take a shortcut and it's why i never use such a loop.
I googled up a thread on another site that suggests DispatchMessage will never actually be called. Read it to find out why.
@rip-off
Yes, the problem is in the if(msg.message == WM_QUIT). The program does not enter into the if statement, I checked it. Thank you.
@Splinter of Chaos
The link you posted has a different context. The DispatchMessage is not called because the program uses GetMessage() with returns 0 when WM_QUIT is get from the queue. In my case, I'm using PeekMessage() and will always call the DispatchMessage() when there is a message in the queue.
But yeah, I got your point in the earlier statement. I should just use a variable flag to check when WM_QUIT is in the queue. Thanks!
But still the process is not ending. Isn't the PostQuitMessage() supposed to insert the WM_QUIT message to the message queue. It seems that the WM_QUIT is never inserted.
I also tried this code to check whether the WndProc() receives the WM_QUIT message. But the message box is not showing, which means it never receives the WM_QUIT;
WM_QUIT isn't sent to a particular window. Your message loop never receives it because it only handles messages specific to the window you created. Replace the hWnd parameter with 0.