Thread: Why Isn't My Application Terminating?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    16

    Why Isn't My Application Terminating?

    I've written a simple Windows program that will display a window. However, when I close the window it closes properly but the application is still running in the background.

    Code:
    #include <windows.h>	
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_CLOSE:			
    		DestroyWindow(hwnd);		
    		
    		PostQuitMessage(0);			
    		return 0;
    		break;
    	}
    
    	return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	bool done = false;
    	MSG msg; 
    
    
    	LPCTSTR className = "ClassName";	
    	WNDCLASSEX wc;						
    		wc.cbSize = sizeof(WNDCLASSEX);
    		wc.style = CS_HREDRAW | CS_VREDRAW;				
    		wc.lpfnWndProc = WndProc;						
    		wc.cbClsExtra = 0;								
    		wc.cbWndExtra = 0;								
    		wc.hInstance = hInstance;						
    		wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);		
    		wc.hCursor = LoadCursor(NULL, IDC_CROSS);		
    		wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);	
    		wc.lpszMenuName = NULL;							
    		wc.lpszClassName = className;				
    		wc.hIconSm = LoadIcon(NULL, IDI_WARNING);		
    	
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Error Registering Windows Class!", "Error!", MB_OK | MB_ICONERROR);
    		return 1;
    	}
    
    	HWND hwnd = CreateWindowEx(
    		0,								
    		className,						
    		"Window Title",					
    		WS_OVERLAPPEDWINDOW,			
    		CW_USEDEFAULT, CW_USEDEFAULT,	
    		300, 300,						
    		NULL,							
    		NULL,							
    		hInstance,						
    		NULL							
    	);
    
    	ShowWindow(hwnd, nShowCmd);	
    	UpdateWindow(hwnd);
    	
    
    	while(!done)
    	{
    		PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
    
    		if(msg.message == WM_QUIT)
    		{
    			done = true;
    		}
    		else
    		{
    			
    			TranslateMessage(&msg);		
    			DispatchMessage(&msg);		
    		}
    	}
    
    	return msg.wParam;  
    }
    Any insight on why this application doesn't close? I thought PostQuitMessage() would take care of this.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Handle WM_DESTROY instead of WM_CLOSE.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    16
    Quote Originally Posted by xeddiex
    Handle WM_DESTROY instead of WM_CLOSE.
    I changed my case statement to WM_DESTROY and the application still persists in the background after the window has been destroyed.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Of course your handling of the messages is wierd, but it is not the source of the problem. Just for proper style points we would re-arrange your WinProc to look something like:

    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    		case WM_CLOSE: 
    			DestroyWindow(hwnd); 
    			return 0; 
     
    		case WM_DESTROY:
    			PostQuitMessage(WM_QUIT); 
    			return 0;
    	}
    	return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    The real problem was actually caused in your message loop. You should re-arrange that to look something like:

    Code:
    	MSG msg;
    	while(true)
    	{
    		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
    		{
    			if(msg.message == WM_QUIT)
    				break;
    
    			TranslateMessage( &msg );
    			DispatchMessage( &msg );
    		}
    	}
    Last edited by Tonto; 09-24-2005 at 04:17 PM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    16
    Thanks for the clarification! It works flawlessly now.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Unless you have a good reason, you should use GetMessage instead of PeekMessage:
    Code:
    	while ( GetMessage(&msg, NULL, 0, 0) )
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return (int) msg.wParam;

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    16
    Quote Originally Posted by anonytmouse
    Unless you have a good reason, you should use GetMessage instead of PeekMessage:
    I'm making an OpenGL application and if what I read was correct on MSDN, PeekMessage() doesn't wait for a message before returning. This speeds up my rendering immensely.

  8. #8
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I'm making an OpenGL application and if what I read was correct on MSDN, PeekMessage() doesn't wait for a message before returning.
    That's correct.

    This speeds up my rendering immensely.
    It doesn't actually speed up your rendering, it just increases the number of times you render.

    It probably takes up a lot of CPU, probably 100% if you're not careful. Normally I just render when I need it by using a timer-based system and Sleep()-ing the unnecessary time away.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    16
    Quote Originally Posted by Dante Shamest
    It doesn't actually speed up your rendering, it just increases the number of times you render.
    Ah, alright, I didn't even think to make that distinction.

    It probably takes up a lot of CPU, probably 100% if you're not careful. Normally I just render when I need it by using a timer-based system and Sleep()-ing the unnecessary time away.
    That being said, is it possible to know whether a window has been minimized to the taskbar? I would like to stop calling my rendering routine if the window has been minimized, then resume rendering when the window has been re-opened.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cleanup of the application...
    By Petike in forum Windows Programming
    Replies: 1
    Last Post: 08-16-2008, 05:23 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Problem with com application
    By amardon in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2005, 05:50 AM
  4. MFC run application by clicking on file...
    By dug in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 04:33 AM