Thread: What is wrong with this simple program? runs in the background and takes all of cpu

  1. #1
    Shadow12345
    Guest

    What is wrong with this simple program? runs in the background and takes all of cpu

    I suspect that for some reason the window isn't being destroyed properly. I have done many simple windows programs like this in the past and I am not sure why this one all of a sudden is acting up on me. It keeps running in the background even when I close the window, and it uses %100 of my processor so everything else in windows runs really slow.

    Code:
    #include <windows.h>
    
    
    LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){
    	HWND Uber;
    	MSG msg;
    	WNDCLASSEX wc;
    	wc.cbClsExtra = 0;
    	wc.cbSize = sizeof(WNDCLASSEX);
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	wc.hCursor = LoadCursor(hInstance, IDC_CROSS);
    	wc.hIcon = LoadIcon(hInstance, IDI_ERROR);
    	wc.hIconSm = NULL;
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = MainWndProc;
    	wc.lpszClassName = "Main Window";
    	wc.lpszMenuName = "Main Window";
    	wc.style = CS_HREDRAW | CS_VREDRAW;
    	
    	if(!RegisterClassEx(&wc)) {
    		MessageBox(NULL, "Could not initialize window", "ERROR", MB_OK);
    		return ERROR;
    	}
    	
    	if(!(Uber = CreateWindowEx(NULL, "Main Window", "Main Window", WS_SYSMENU |  WS_MAXIMIZEBOX | WS_MINIMIZEBOX, 
    							   0, 0, 
    							   CW_USEDEFAULT, CW_USEDEFAULT, 
    							   NULL, NULL, hInstance, NULL))) {
    							   MessageBox(NULL, "Could not initialize window", "ERROR", MB_OK);
    								return ERROR;						   
    	}
    	
    	ShowWindow(Uber, nShowCmd);
    	UpdateWindow(Uber);
    
    	while(GetMessage(&msg, Uber, 0, 0)) { 
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return 0;
    }
    
    LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    	switch(message) {
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    }

  2. #2
    Jim(U)
    Guest

    Question

    Your code looks pretty neat to me..

    And I just guess here but try to replace the WM_DESTROY with WM_QUIT

    As PostQuitMessage sends a WM_DESTROY to your window... which kind of results in an infinite loop loop loop loop loop loop

  3. #3
    Jim(U)
    Guest
    OOPS... forget about it

    /ignore me - sorry

  4. #4
    Jim(U)
    Guest

    Talking

    What about this idea ?

    WM_DESTROY is never received by your app as you never call DestroyWindow ?

    / I better stop rambling /

    LOL

  5. #5
    Shadow12345
    Guest
    Hmm I was thinking that my code looked fine too! I guess I need to just kick my computer or something because I cannot figure out why it is still running in the background even when I close it.

    PostQuitMessage() generates a WM_QUIT, and then WM_QUIT message in turn is supposed to supply a false condition thus ending the loop.

    Oddly enough I just changed the second parameter in GetMessage from Uber (My Uber(Ultimate Parent) Window) to null so that it now looks like:
    Code:
    	while(GetMessage(&msg, NULL, 0, 0)) {
    I am not done though, I would like to know exactly why changing that second parameter to NULL works, but putting in an instance of HWND doesn't.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    if in doubt read the help.

    The NULL tell GetMessage() to get all messages for ALL windows in the app not just the main screen. This would include all child controls ON the main screen.

    GetMessage() will not return until there is a message for that HWND so your app was probably ignoring most of the messages for it. Particularly if the HWND you used had lost scope.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    One more thing....GetMessage can return three "types" of values: 0 (quitting), positive (getting a message), or negative (big time error). Thus, you should say while(GetMessage(/**/) > 0).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed