Jump to content
  • Advertisement

Archived

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

lackluster

CPU 99%

This topic is 5879 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!