C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 16th, 2003, 07:07 PM
movEAX_444's Avatar
movEAX_444 movEAX_444 is offline
Cast down
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Sweden
Posts: 321 movEAX_444 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 56 m 35 sec
Reputation Power: 6
win32 window process keeps running

When I close it, the window closes, but the process keeps running and I have to end task it:

Code:
#define WIN32_LEAN_AND_MEAN 1

#include <windows.h> 
//#include <gl/gl.h> 
//#include <gl/glu.h> 
//#include "MyGL.h" 

char szApp[]       = "First OpenGL ****"; 
char szClassname[] = "FirstOpenGL"; 
char szFilename[MAX_PATH]; 

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{
	if(uMsg==WM_DESTROY) 
	{
		PostQuitMessage(0); 
		return 0; 
	}
	else if(uMsg==WM_CREATE)
	{
		//
	}
	else if(uMsg==WM_SIZE)
	{ /*
		int height, width; 
		height=HIWORD(lParam); 
		width = LOWORD(lParam); 
		if(height==0)
		{
			height=1; 
		} */
		/*
		glViewport(0,0,width,height); 
		glMatrixMode(GL_PROJECTION); 
		glLoadIdentity(); 
		
		gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.f); 

		glMatrixMode(GL_MODELVIEW); 
		glLoadIdentity(); 
		*/
	}
	else 
		return DefWindowProc(hwnd,uMsg,wParam,lParam); 
	return 0; 
}

WPARAM WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int CmdShow)
{
	WNDCLASSEX	wc; 
	MSG			msg; 
	HWND		hwnd; 
	
	/* 
		OpenGL specific
	*/
	HDC		hDc=NULL; 
	HGLRC	hRc=NULL; 

	wc.cbSize			= sizeof(WNDCLASSEX); 
	wc.cbClsExtra		= 0; 
	wc.cbWndExtra		= 0; 
	wc.hbrBackground	= (HBRUSH)(COLOR_BTNFACE+1); 
	wc.hCursor			= LoadCursor(NULL,IDC_ARROW); 
	wc.hIcon   = wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION); 
	wc.hInstance		= hInst; 
	wc.lpfnWndProc		= WndProc; 
	wc.lpszClassName	= szClassname; 
	wc.lpszMenuName		= NULL; 
	wc.style			= CS_OWNDC; 

	if(!RegisterClassEx(&wc)) 
	{
		MessageBox(NULL,"Son... an error occured at RegisterClassEx()",szApp,MB_OK); 
		return 0; 
	} 

	hwnd = CreateWindowEx(
					0, 
					szClassname, 
					szApp, 
					WS_OVERLAPPEDWINDOW, 
					CW_USEDEFAULT, 
					CW_USEDEFAULT, 
					800, 
					500, 
					NULL, 
					NULL, 
					hInst, 
					NULL); 

	if(hwnd==NULL)
	{
		MessageBox(NULL,"Son... an error occured at CreateWindowEx()",szApp,MB_OK); 
		return 0; 
	} 

	ShowWindow(hwnd,CmdShow); 
	UpdateWindow(hwnd); 

	while(1)
	{
		if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
			if(msg.message == WM_QUIT) 
				break; 
			else
			{
				//RenderScene(); 
				//SwapBuffers(); 
				TranslateMessage(&msg); 
				DispatchMessage(&msg); 
			}
	} 
	return msg.wParam; 
}

What's wrong with this? It worked until I changed GetMessage to PeakMessage

Reply With Quote
  #2  
Old July 16th, 2003, 08:18 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
Re: win32 window process keeps running

I guess I have no answer to your question, except some notes on your program:

I'd like to know where you got the code for your message loop from:

Code:
	while(1)
	{
		if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
			if(msg.message == WM_QUIT) 
				break; 
			else
			{
				//RenderScene(); 
				//SwapBuffers(); 
				TranslateMessage(&msg); 
				DispatchMessage(&msg); 
			}
	} 
	return msg.wParam; 
}


because that is not normal. This is what the standard message loop looks like:

Code:
BOOL bRet;

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
}


even though most people do NOT check for the possibility of an error from GetMessage, so they incorrectly use this:

Code:
while( (GetMessage( &msg, NULL, 0, 0 )) )
{ 
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
}


which is ok, if you want a version of your program that doesn't bother checking for errors to make things simplier to understand.

Notice how the look ends without a call to break; This is because GetMessage() returns 0 (false) when it retrieves the WM_QUIT message.

Although... PeekMessage() always retrieves WM_QUIT messages, so there seems like there is nothing wrong with your code...

Another note: You could use a switch statement in the WndProc() function, instead of all those if ... else if ... else if.

Last edited by Jason Doucette : July 16th, 2003 at 08:28 PM.

Reply With Quote
  #3  
Old July 16th, 2003, 08:26 PM
movEAX_444's Avatar
movEAX_444 movEAX_444 is offline
Cast down
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Sweden
Posts: 321 movEAX_444 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 56 m 35 sec
Reputation Power: 6
I got it from a directX book, getMessge() is too slow for games, so it uses PeekMessage().


I commented out the code below and changed it to a standard messagebox, and it doesn't hang on exit.. So the problem is with the msgloop, what is wrong with it though? In my WndProc, I only handle WM_DESTROY as PostQuitMessage(0).

Code:
/*
	while(1)
	{ 
		if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
			if(msg.message == WM_QUIT) 
				break; 
			else
			{
				//RenderScene(); 
				//SwapBuffers(); 
				TranslateMessage(&msg); 
				DispatchMessage(&msg); 
			}
	}  
*/
while( (GetMessage( &msg, NULL, 0, 0 ))>0)
{ 
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
}

Last edited by movEAX_444 : July 16th, 2003 at 08:32 PM.

Reply With Quote
  #4  
Old July 16th, 2003, 08:30 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
The only difference I can see between the two is that PeekMessage is documented as follows:

"During this call, the system delivers pending messages that were sent to windows owned by the calling thread using the SendMessage, SendMessageCallback, SendMessageTimeout, or SendNotifyMessage function. The system may also process internal events. Messages are processed in the following order:

Sent messages
Posted messages
Input (hardware) messages and system internal events
Sent messages (again)
WM_PAINT messages
WM_TIMER messages
"

I cannot test the code at the moment, so I am sorry that I cannot help you much. Perhaps the above has something to do with your problem.

Reply With Quote
  #5  
Old July 16th, 2003, 08:35 PM
movEAX_444's Avatar
movEAX_444 movEAX_444 is offline
Cast down
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Sweden
Posts: 321 movEAX_444 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 56 m 35 sec
Reputation Power: 6
I just fixed it, here is how it looks now:

Code:
	while(1)
	{ 
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT) 
				break; 

			//RenderScene(); 
			//SwapBuffers(); 
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		}
	}  

Reply With Quote
  #6  
Old July 16th, 2003, 09:06 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
Aha. Good job! I did not notice that you were calling it with the window handle before. I didn't think to compare the arguments with arguments in GetMessage() to see what the differences / similarities are.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > win32 window process keeps running


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT