CPU going to 100% upon exiting

This is a discussion on CPU going to 100% upon exiting within the Windows Programming forums, part of the Platform Specific Boards category; Ok, I'm stumped on this one, maybe someone else can catch whatever little error I have in this program. Basically ...

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    CPU going to 100% upon exiting

    Ok, I'm stumped on this one, maybe someone else can catch whatever little error I have in this program. Basically whats happening is the program works fine while running, but the instant it closes my CPU goes to 100% and I have to go to task manager and end the process manually. I've commented everything out but the essentials yet it is still doing it, any help? Heres the code:
    Code:
    #include<windows.h>
    //#include<math.h>
    //#include<time.h>
    //#include<queue>
    //#include "resource.h"
    //using namespace std;
    
    
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    //BOOL CALLBACK MainDialogProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
    {
    	//HMENU hMenu;
    	//srand(time(NULL));
    	static TCHAR szAppName[]=TEXT("Assessment Runner");
    	HWND hwnd;   // handle to a window
    	MSG msg;   // message
    	WNDCLASS wndclass;  // window class we are declaring
    
    
    	// Now we set the attributes for our window
    	wndclass.style=CS_HREDRAW | CS_VREDRAW;  // Style, in this case can be redrawn
    	wndclass.lpfnWndProc=WndProc;  // window process name - handles message calls
    	wndclass.cbClsExtra=0;  // Extra space for the window
    	wndclass.cbWndExtra=0;  // ditto
    	wndclass.hInstance=hInstance;  // program instance name passed to WinMain
    	wndclass.hIcon=LoadIcon(NULL, IDI_APPLICATION);  // Load icon for the tray
    	wndclass.hCursor=LoadCursor(NULL, IDC_ARROW);  // Load icon for the mouse arrow
    	wndclass.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);  // Load background color
    	wndclass.lpszMenuName=NULL;  // Set menu - none here
    	wndclass.lpszClassName=szAppName;  // Sets window name, taken from above
    
    	//hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
    
    	if (!RegisterClass(&wndclass))
    	{
    		MessageBox(NULL, TEXT("Error running program!"), szAppName, MB_ICONERROR);
    		return 0;
    	}
    
    
    
    	hwnd=CreateWindow(szAppName, TEXT("Assessment Runer"), WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, 0,
    		hInstance, NULL);  // Create the window!  Name, Title, Style, x Position, y Position
    	                       // X size, Y size, parent window handle, window menu handle, 
    						   // program instance handle, creation parameters(passing variables)
    
    
    	ShowWindow(hwnd, iCmdShow);  // now show the window!
    	UpdateWindow(hwnd);   // update it
    
    	while (GetMessage(&msg, hwnd, 0, 0))  // gets message from mouse and keyboard, always non
    		                                  // zero until you quit the window
    	{
    		TranslateMessage(&msg);           // translates message from message queue (mouse/keyboard/etc)
    		DispatchMessage(&msg);			  // sends to WndProc to translate and tell what to do!
    	}
    
    	return msg.wParam;
    
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	//static HINSTANCE hInstance;
    
    	switch (message)
    	{
    	/*case WM_CREATE:
    		hInstance=((LPCREATESTRUCT) lParam)->hInstance;
    		return 0;
    
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case ID_TEST_TEST:
    			DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, MainDialogProc);
    			break;
    		}
    
    		return 0;*/
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	}
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }
    
    
    /*
    BOOL CALLBACK MainDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static HWND hwndCombo;
    
    	switch (message)
    	{
    	case WM_INITDIALOG:
    		hwndCombo=GetDlgItem(hDlg, IDC_COMBO1);
    		SendMessage(hwndCombo, CB_ADDSTRING,0,(LPARAM) "Test");
    		SendMessage(hwndCombo, CB_ADDSTRING,0,(LPARAM) "Test2");
    		SendMessage(hwndCombo, CB_ADDSTRING,0,(LPARAM) "Test3");
    		SendMessage(hwndCombo, CB_ADDSTRING,0,(LPARAM) "Test4");
    		return TRUE;
    
    	case WM_COMMAND:
    		switch (LOWORD (wParam))
    		{
    		case IDC_COMBO1:
    			return TRUE;
    		case IDOK:
    			EndDialog(hDlg, TRUE);
    			return TRUE;
    		}
    		break;
    	}
    
    	return FALSE;
    }*/

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	while (GetMessage(&msg, hwnd, 0, 0))
    GetMessage returns -1 if it fails. Typically, GetMessage will fail because of an invalid argument, such as a window handle that no longer points to a window.

    You should use NULL for the hwnd parameter of GetMessage. This lets it return messages for all windows belonging to the thread as well as thread messages. See the documentation for an example of error handling for the GetMessage function.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    D'oh, thanks, you're absolutely right. I was having problems with combo boxes showing and I saw a post from a little while ago where someone else had it changed to hwnd so I tried it and forgot to change it back. Thanks for the help!

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,668
    Because you're using hwnd in the call to GetMessage(), the function is failing before it has a chance to pull out the WM_QUIT and return 0.

    When GetMessage() fails, due to an invalid HWND for example, it will return -1, not 0. So you keep on loop'n.

    The quick fix is to use NULL for the HWND in the call to GetMessage(), that way there's no invalid HWND to fail on. The correct fix is to implement a message loop like the example in the GetMessage() documentation.

    gg

    [edit]
    Dang.....beaten badly....
    [/edit]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question
    By quantt in forum Linux Programming
    Replies: 7
    Last Post: 04-07-2009, 01:21 AM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  3. Upgrading my old CPU (for another old one!)
    By foxman in forum Tech Board
    Replies: 16
    Last Post: 01-11-2008, 04:41 PM
  4. Can you still view the bios screen with a bad CPU?
    By HyperCreep in forum Tech Board
    Replies: 4
    Last Post: 12-31-2006, 05:57 PM
  5. CPU temp
    By PING in forum Tech Board
    Replies: 5
    Last Post: 01-28-2006, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21