Skip to main content
GameDev.net gamedev.net
🔒 Locked

CPU 99%

Started by lackluster Apr 17, 2002 at 10:55 PM 11 replies 750+ views
Original Post
lackluster
lackluster
It''s probably a small detail I''m overlooking, but I can''t figure out why everytime I run a simple window (well, with this prog at least :/) it makes the CPU time jump up to 99%. Has anybody seen this symptom before? One time it happend because I had the DefWindowProc in the wrong place, but this time I don''t think that''s the case. THe bottom portion of my WndProc: ---------------------------------- default: return(DefWindowProc(hwnd, uMsg, wParam, lParam)); } return 0; } My MsgLoop: --------------------------------- MSG msg; while(true) { PeekMessage(&msg, hWnd, NULL, NULL, PM_REMOVE); if (WM_QUIT == msg.message) { return msg.wParam; } TranslateMessage(&msg); DispatchMessage(&msg); } any thoughts? thanks in advance, lackluster
outRider
outRider
You''re probably not handling messages properly and they''re piling up. But try this: in your winproc add

case WM_ACTIVATE:
{
if (LOWORD(wParam) == WA_INACTIVE)
bWindowActive = false;
else
bWindowActive = true;
break;
}

if (!bWindowActive)
WaitMessage();

------------
- outRider -
IndirectX
IndirectX
First of all, you should check PeekMessage return value and only dispatch the message if you actually received it.

Second of all, that loop is constantly executing, which is why you get 99% CPU usage. "Normal" apps (ie, not games) use GetMessage or WaitMessage to sleep while there are no messages available. Games, however, do everything game-related when there are no messages. Therefore, each game runs at 100% CPU usage.

You should, however, not use 100% CPU when your app is in background. Use one of the functions above to wait for messages when your app is inactive, which you can find out by handing WM_ACTIVATE.
---visit #directxdev on afternet <- not just for directx, despite the name
Dean Harding
Dean Harding
WM_ACTIVATE is only sent when the user clicks on your window, or when they click on another window (of another app) while yours has focus. It''s got nothing to do with whether there are messages waiting or not.

Unless you want to do something while there''s nothing waiting on the message queue (i.e. if it''s a game, you''ll want to draw the current frame, do the physics, etc) then use GetMessage() instead of PeekMessage().


codeka.com - Just click it.
logicpill
logicpill
You could also use a sleep(0) to dramatically drop the CPU usage. Just make sure it gets called at the end of every loop, or something close to that.
DrPizza
DrPizza
quote:
Original post by Pik
You could also use a sleep(0) to dramatically drop the CPU usage. Just make sure it gets called at the end of every loop, or something close to that.


Or you could fix whatever problem is actually fucking your code up.

I know which I''d prefer.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
Sand_Hawk
Sand_Hawk
You have a realtime message loop. If you are making an office app you shouldn't make it realtime but only process when a message is in queue. The msg loop should look like this:


  
while (GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}


Compile and watch how your CPU is dropping. At least that is what happens on my PC.

Sand Hawk

Member of the Stupid Coders.
http://www.stupidcoders.cjb.net

-Earth is 98% full. Please delete anybody you can.

[edited by - sand_hawk on April 18, 2002 3:09:39 PM]
----------------(Inspired by Pouya)
merlin9x9
merlin9x9
quote:

WM_ACTIVATE is only sent when the user clicks on your window

If I''m not mistaken, it''s more general than that; the message is received by your window procedure any time that window gains the input focus. wParam allows you to determine whether or not this was by a click.
Oluseyi
Oluseyi
Sand_Hawk:
That''s a malformed message loop, as GetMessage can conceivably return -1 - which would keep your loop executing but is a terminal error. Either use while( GetMessage(..) == 1 ) or place the call to GetMessage inside the loop.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
lackluster
lackluster
Thanks for all the replies.

I am actually making the base to learn OpenGL and that''s why I picked PeekMessage(). I guess I wasn''t aware that it would skyrocket my CPU time if I didn''t do some processing in there. I guess I shouldn''t have obsessed over it so much and just gone ahead with some of the OpenGL tuts instead .
Shannon Barber
Shannon Barber
For the time being, at least add a sleep(0). This will still run your CPU at 100%, but it will reliquish your time-slice if something else needs to do some work. You could also take the mutlithreaded approach, and use GetMessage in the pump and a seperate thread for the game.
You don''t want to add a sleep(10), because they introduces artifical latency in your message handling.


quote:

You have a realtime message loop


There''s no such thing as a real-time anything with windows. You know how they have those API calls to set the process and thread priorities to ''real-time''? Yeah, they''re just kidding. Windows was designed to make office applications, not send rockets to moon
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Dean Harding
Dean Harding
quote:
Original post by merlin9x9
If I''m not mistaken, it''s more general than that; the message is received by your window procedure any time that window gains the input focus. wParam allows you to determine whether or not this was by a click.


That''s correct, though my point was more that it wasn''t sent when no more messages are available, which is what seemed to be implied by the previous post...


codeka.com - Just click it.
outRider
outRider
quote:
That's correct, though my point was more that it wasn't sent when no more messages are available, which is what seemed to be implied by the previous post...


Sorry, that's not what I was implying. When I mentioned messages piling up it was just a thought and was unrelated to the second part of my post. The reason I suggested WM_ACTIVATE was that when he went to check his CPU usage in TaskMan or wherever his app would have lost focus, and then it could wait for messages and not endlessly loop and keep peeking for messages.

------------
- outRider -

[edited by - outRider on April 19, 2002 9:04:23 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.