Sign in to follow this  
Rottingham

Program Running 99% CPU Usage, No Window

Recommended Posts

Rottingham    122
My basic empty window template will not run on my laptop! I wonder if I did something different... The program runs in the background, I have to CTRL + ALT + DEL to close it and it's running 99% CPU usage although it doesn't tack my computer down. Somewhere it is stuck in a loop and the only loop there is in StartMessageLoop but I can not find anything wrong with it! Can anyone else see a flaw??
//////////////////////////////////////////////////////
// BasicDirect3DApp 
//////////////////////////////////////////////////////

#include <windows.h>

// Function Prototypes
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); void RegisterWindowClass(HINSTANCE hInstance); void CreateAppWindow(HINSTANCE hInstance); WPARAM StartMessageLoop();

// Global variables.
HWND g_hWnd;

//////////////////////////////////////////////////////
// WinMain
//////////////////////////////////////////////////////
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT) {
	RegisterWindowClass(hInstance);
	CreateAppWindow(hInstance);
	ShowWindow(g_hWnd, SW_SHOWDEFAULT);
	UpdateWindow(g_hWnd);
	INT result = StartMessageLoop();

	return result;
}

//////////////////////////////////////////////////////
// WndProc 
//////////////////////////////////////////////////////
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch(msg)
	{
	case WM_CREATE:
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_PAINT:
		ValidateRect(g_hWnd, NULL);
		return 0;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

//////////////////////////////////////////////////////
// RegisterWindowClass
//////////////////////////////////////////////////////
void RegisterWindowClass(HINSTANCE hInstance)
{
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "Basic Direct3D App";
	wc.hIconSm = NULL;

	RegisterClassEx(&wc);
}

//////////////////////////////////////////////////////
// CreateAppWindow 
//////////////////////////////////////////////////////
void CreateAppWindow(HINSTANCE hInstance)
{
	g_hWnd = CreateWindowEx(NULL, "WinApp", "Basic Direct3D App",
							WS_OVERLAPPEDWINDOW, 100, 100, 648, 514,
							GetDesktopWindow(), NULL, hInstance, NULL);
}

//////////////////////////////////////////////////////
// StartMessageLoop 
//////////////////////////////////////////////////////
WPARAM StartMessageLoop()
{
	MSG msg;
	while(1)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			// Proccess during idle time
		}
	}

	return msg.wParam;
}


Share this post


Link to post
Share on other sites
I think your problem is that you aren't calling Sleep() anywhere. Until you exit your main loop (which closes the window I think) the computer will repeat the loop over and over again as fast as it can.

EDIT: Oops. Didn't see the No Window part of the topic title. Disregard this post (I think).

Share this post


Link to post
Share on other sites
VolkerG    151
Parameter 2 of CreateWindowEx is the class name, number 3 is the window name. You name your class "Basic Direct3D App", so you have mixed the parameters.
99% CPU usage is normal when you use peek message.

Share this post


Link to post
Share on other sites
Rottingham    122
Thank you all!! I didn't even realize I had the class names mixed up! I'm pretty rusty after not doing any of this for 3 years!

It's up and showing me my blank window! Thanks again guys! Good eyes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this