Computer grinds to a halt

Started by
4 comments, last by Kris_A 18 years, 9 months ago
Hi, I'm making a small framework for making Direct3D games (mainly just for myself), called DXW. I seem to be having a weird problem in which my programs use up the entire CPU's time and don't let any other programs use any (so it takes around 5 minutes to close the damn thing when it locks up). I found out that calling 'Sleep(1)' fixes it, but I'm positive that last time I made a framework (recoding because I lost it in a HD crash), I didn't need to call that. It used 99% CPU time but it didn't 'hog' like it's doing now. This is the code in question:
while (DXW_HandleMessages()) {
	dxw_device->Clear(0,NULL,D3DCLEAR_TARGET,0x00000000,1.0f, 0);
	dxw_device->BeginScene();
	dxw_device->EndScene();
	dxw_device->Present(0,0,0,0);
	//Sleep(1); 
}
And here's DXW_HandleMessages() :
bool DXW_HandleMessages() {
	MSG msg;

	while (PeekMessage(&msg,dxw_window,0,0,PM_REMOVE)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
		
		while (PeekMessage(&msg,0,WM_QUIT,WM_QUIT,PM_REMOVE)) {
			if (msg.message == WM_QUIT) return false;
		}
	}
	
	return true;
}
This is my initialisation code:

memset(&params,0,sizeof(params));
params.BackBufferWidth = width;
params.BackBufferHeight = height;
params.BackBufferFormat = format;
params.MultiSampleType = D3DMULTISAMPLE_NONE;
params.MultiSampleQuality = 0;
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
params.hDeviceWindow = window;
params.Windowed = !fullscreen;
params.EnableAutoDepthStencil = false;
params.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
params.Flags = 0;
params.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
params.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
result = dxw_d3d->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,
				window,
				D3DCREATE_HARDWARE_VERTEXPROCESSING,
				&params,
				&dxw_device);

I'm guessing that some of the initialisation parameters I've used this time may be different to the ones I used last time. Any ideas? Thanks, Kris [Edited by - Kris_A on July 2, 2005 10:54:16 AM]
Advertisement
Here's my message handler:

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;

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

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

You need to place a DefWindowProc command in there, otherwise all other messages that you didn't process stay in the message loop, slowing down your program considerably.

Hope this works, send me a private message on your results. I may still be able to help you.
Quote:Original post by Kris_A
bool DXW_HandleMessages() {
MSG msg;

while (PeekMessage(&msg,dxw_window,0,0,PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);

while (PeekMessage(&msg,0,WM_QUIT,WM_QUIT,PM_REMOVE)) {
if (msg.message == WM_QUIT) return false;
}
}

return true;
}


There's no need for the internal while loop. Do something like this:
MSG msg;while (PeekMessage(&msg,dxw_window,0,0,PM_REMOVE)){    if(WM_QUIT == msg.message)        return false;    TranslateMessage(&msg);    DispatchMessage(&msg);    return true;}

I tried that, and it doesn't work - the MSDN says that the WM_QUIT message isn't sent to a particular window, it's just sent to the program, hence the inner loop with '0' as the window.

Just tried the ValidateRect code - and it WORKS! Thanks! Could anyone explain why adding it actually prevents the lockup?
Quote:Original post by Kris_A
I tried that, and it doesn't work - the MSDN says that the WM_QUIT message isn't sent to a particular window, it's just sent to the program, hence the inner loop with '0' as the window.

Ah, apologies. I thought your outer loop had a 0 window parameter.

Quote:Just tried the ValidateRect code - and it WORKS! Thanks! Could anyone explain why adding it actually prevents the lockup?

If you don't validate the window rectangle, windows keeps sending your application WM_PAINT messages.

EDIT: And also if you weren't delegating the messages you didn't handle to DefWindowProc, they'd stay in the message queue.

Ah, makes sense now... thanks a bunch everyone!

This topic is closed to new replies.

Advertisement