Window not closing and taking 100% of processor

This is a discussion on Window not closing and taking 100% of processor within the Windows Programming forums, part of the Platform Specific Boards category; Alright I know this is just a window with black background but I am starting a windows tic tac toe ...

  1. #1
    Unregistered
    Guest

    Window not closing and taking 100% of processor

    Alright I know this is just a window with black background but I am starting a windows tic tac toe I already have one written but lost the source code in a reformat of a hard drive. Alright after the window closes it takes up 100% of processor in my task manager and doesn't close down anybody see any errors in my code.

    Code:
    #include <windows.h>
    
    static char szApp[]	= "win32ttt";
    static int	grid[9];
    static int	cyClient, cxClient;
    static int	R, G, B;
    static int	turn	= 1;
    void DrawGrid(HWND, HDC, HPEN);
    void DrawX(HWND, HDC, HPEN, int, int, int, int);
    void DrawO(HWND, HDC, HPEN, int, int);
    void Check(int, HWND);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	WNDCLASS		wc;
    	HWND			hWnd;
    	MSG				message;
    
    	wc.style		= CS_VREDRAW | CS_VREDRAW;
    	wc.lpfnWndProc	= WndProc;
    	wc.cbClsExtra	= 0;
    	wc.cbWndExtra	= 0;
    	wc.hInstance	= hInstance;
    	wc.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon		= LoadIcon(NULL, IDC_ARROW);
    	wc.hbrBackground= (HBRUSH) GetStockObject(BLACK_BRUSH);
    	wc.lpszMenuName	= NULL;
    	wc.lpszClassName= szApp;
    	cxClient		= 400;
    	cyClient		= 450;
    
    	if(!RegisterClass(&wc))
    	{
    		MessageBox(NULL, "Could Not Register Class", "Windows Tic Tac Toe", MB_OK);
    		return 0;
    	}
    
    	hWnd = CreateWindow(szApp,
    						"Windows Tic Tac Toe",
    						WS_OVERLAPPEDWINDOW,
    						CW_USEDEFAULT,
    						CW_USEDEFAULT,
    						400,
    						450,
    						NULL,
    						NULL,
    						hInstance,
    						NULL);
    
    	ShowWindow(hWnd, nShowCmd);
    	UpdateWindow(hWnd);
    
    	while(GetMessage(&message, hWnd, 0, 0))
    	{
    		TranslateMessage(&message);
    		DispatchMessage(&message);
    	}
    	return message.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch(message)
    	{
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	}
    
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You aren't unregistering your class, try this I that it will fix the problem.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    In your message loop, the call to GetMessage(), change the second parameter from hWnd to NULL.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Adrianxw is right.
    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;
    }

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I believe he needs to do this also... Straight from MSDN
    Code:
    This function removes a window class, freeing the memory required for the class.
    
    BOOL UnregisterClass(
    LPCTSTR lpClassName,
    HINSTANCE hInstance
    );

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Why?....the class needs to be registered throughout the life of the app......by the time it is not needed the app is ending and so any class registrations will be automatically unregistered when the app terminates.......

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Adrianxw is right.
    Fordy is right, too.

    From msdn description of RegisterClass (now superceded by RegisterClassEx):
    All window classes that an application registers are unregistered when it terminates.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay I see. Thank you for the clarification. I just have one question. Why would they include that function if it is already done for you? For historical reasons?

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    probably just in case you wanted to unregister it earlier or something... (maybe if you're REALLY running out of memory )
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  2. OpenGL rendering problem
    By JJFMJR in forum Game Programming
    Replies: 8
    Last Post: 08-31-2007, 08:26 PM
  3. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM

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