Jump to content
  • Advertisement
Sign in to follow this  
Uphoreum

[Win32] Strange Behavior

This topic is 4134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a small loop that, while done = false, runs ProcessMessages. What's happening is that, even if done = true, it still runs the loop! Here's the loop code:
while(!done)
{
	ProcessMessages(msg);
}

and the "ProcessMessages" code:
void ProcessMessages(MSG &msg)
{
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

and WinProc:
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		done = true;
		break;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

I checked the state of "done" using the debugger while the app was running, and it was 'true', yet the loop continued to run. Theres probably something very simple that i'm overlooking, can anyone see it?

Share this post


Link to post
Share on other sites
Advertisement
Yep, I only have one done variable. I'll just post the whole code if anyone wants to see all of it:


#include <windows.h>

bool done = false;

LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
done = true;
break;
}

return DefWindowProc(hWnd, msg, wParam, lParam);
}

bool Initialize(HINSTANCE hInstance, int nCmdShow)
{
//Create new window class.
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wc.hIconSm = NULL;
wc.hInstance = hInstance;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.lpszClassName = L"WindowClass";
wc.style = CS_HREDRAW | CS_VREDRAW;

//Register the window class.
if(!RegisterClassEx(&wc))
return false;

HWND hWnd = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
L"WindowClass",
L"My Window",
WS_OVERLAPPEDWINDOW,
0,
0,
800,
600,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
}

void ProcessMessages(MSG &msg)
{
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;

if(!Initialize(hInstance, nCmdShow))
{
return EXIT_FAILURE;
}

while(!done)
{
ProcessMessages(msg);
}

return msg.wParam;
}

Share this post


Link to post
Share on other sites
The problem is that GetMessage(&msg, NULL, 0, 0) never returns false, so the loop in ProcessMessages() never terminates. It only returns false when WM_QUIT is recieved. Call PostQuitMessage(0) when you want to terminate and it should work.

Take a look at:

http://msdn2.microsoft.com/en-us/library/ms644936.aspx

for more info.

Coincidentally, the while(!done) really doesn't do anything. ProcessMessages (when called in WinMain) only terminates when the window closes. For example:


while(!done)
{
ProcessMessages(msg);
// Stuff here only gets executed when window terminates,
// and you set done to true when window terminates,
// so this loop will always run for one iteration.
// You could eliminate it and just call ProcessMessages(msg);
// from main without the while loop and it should work fine.
}


Hope that helps.

[Edited by - Firewalker on January 29, 2007 4:30:50 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!