Skip to main content
gamedev.net
🔒 Locked

Close window does not kill process.

Started by cclements 6 replies 700+ views
Original Post
cclements
cclements
Gurus, I have noticed that if I write my Main Window Loop as follows, closing the window does not kill the process. The process continues to run and consume 100% of the CPU resources.
while(true)
{
   if(PeekMessage(&msg,hWnd,NULL,NULL,PM_REMOVE))
   {
	if(msg.message == WM_QUIT)
	   break;
	else
	   TranslateMessage(&msg);
	DispatchMessage(&msg);
	}
   }
}
 
I have discovered that if I change the Message Loop to this, the program functions as expected. Closing the window also kills the process.
while (GetMessage(&msg, NULL,0,0))
	{ 
			TranslateMessage(&msg);
			DispatchMessage(&msg);
	}
 
I have seen game programming books use the first method. I have *heard* GetMessage is too slow for game programming. Can anyone see anything I am doing wrong in the first code snippet? CHris
No signature files!!! =)
Dave Hunt
Dave Hunt
What does your WM_CLOSE message handler do? You need to send a PostQuitMessage(0) on WM_CLOSE.
apanjocko
apanjocko
but then why does it work in the second case
cclements
cclements
Perfecto!

case WM_CLOSE:
{
DestroyWindow(hWnd);
return(0);

}break;


I changed the DestroyWindow to PostQuitMessage and all is well.

Thanks Dave!!!

[edited by - cclements on November 24, 2003 2:13:17 PM]

[edited by - cclements on November 24, 2003 2:14:27 PM]
No signature files!!! =)
antareus
antareus
You still want to call DestroyWindow in addition to PostQuitMessage if your application should shut down now.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
cclements
cclements
Thanks again guys. This is the single most useful site for amateur game programming on the web. Bar none!

Chris
No signature files!!! =)
chronos
chronos
quote:
Original post by cclements
if(PeekMessage(&msg,hWnd,NULL,NULL,PM_REMOVE))
Your mistake is to call PeekMessage with a non-NULL hWnd parameter. The call should read PeekMessage(&msg, NULL, 0, 0, PM_REMOVE). If you pass it a non-NULL window handle it will only retrieve messages sent to that specific window, ignoring messages sent to other windows and messages not associated with any window.

I'm surprised you were able to fix the problem by introducing a PostQuitMessage call. That should not have fixed the problem; the WM_QUIT message is not associated with any windows and should therefore be ignored by your PeekMessage call.

I've had this problem before. I blame IntelliSense for suggesting hWnd.

[edited by - chronos on November 24, 2003 4:15:29 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.