Sign in to follow this  
zee_ola05

HELP - Process not dying

Recommended Posts

[code]
#include <windows.h>

const LPCWSTR className = L"Window1";
const LPCWSTR titleName = L"Window Title";

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

//fill up wc
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = className;
wc.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
wc.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
wc.hIconSm = wc.hIcon;
wc.hInstance = hInstance;

//register wc
RegisterClassEx(&wc);

DWORD windowStyle = (WS_OVERLAPPEDWINDOW | WS_VISIBLE) & ~WS_THICKFRAME;
hWnd = CreateWindow(className, titleName, windowStyle, 300,300,300,300, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);

MSG msg;
while(true)
{
while(PeekMessage(&msg, hWnd, 0,0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if(msg.message == WM_QUIT)
break;

//do game logic here
}

return msg.wParam;
}

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;
}
[/code]

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!

Share this post


Link to post
Share on other sites
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 [url="http://www.cplusplus.com/forum/windows/38366/"]thread on another site[/url] that suggests DispatchMessage will never actually be called. Read it to find out why.

Share this post


Link to post
Share on other sites
@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!

Share this post


Link to post
Share on other sites
Ooops! I tried doing this

[code]

MSG msg;
bool quitFlag = false;
while(true)
{
while(PeekMessage(&msg, hWnd, 0,0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);

if(msg.message == WM_QUIT)
{
quitFlag = true;
}
}

if(quitFlag)
break;

//do game logic here
}
[/code]

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;

[code]

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
case WM_QUIT:
MessageBox(NULL,
L"QUITTING",
L"QUITTING",
MB_ICONEXCLAMATION | MB_OK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[color=#1C2837][size=2][quote]WM_QUIT isn't sent to a particular window.[/quote][/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]If it is not sent to a particular window, where was it sent?[/size][/color]

Share this post


Link to post
Share on other sites
As mentioned in the [url=http://msdn.microsoft.com/en-us/library/ms644945(v=vs.85).aspx] documentation for PostQuitMessage()[/url] it's sent to the thread message queue.

Share this post


Link to post
Share on other sites
Based on the info [url="http://www.mvps.org/directx/articles/writing_the_game_loop.htm"]here[/url], this is more or less how your loop should look:
[code] MSG msg;

// prime the message
PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);

while (msg.message != WM_QUIT)
{
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
else
{
// game logic and rendering goes here
}
}[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this