• Advertisement

Archived

This topic is now archived and is closed to further replies.

CPU 99%

This topic is 5786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 -

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 .

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement