Jump to content
  • Advertisement

Archived

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

anachronoks

App takes up 100% system usage

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

Hi. I''m new to programming DirectX, and the first thing I noticed after creating a simple program was that all of the CPU''s usage was being eaten up by my program. The Windows Task Manager in Win XP shows usage. At the moment, I''m using the following to synchronize timing to 30 frames per second: myTickCountNow = GetTickCount(); if (myTickCountNow > (myLastTickCount + myTicksBeforeUpdate)) { myLastTickCount = myTickCountNow; // calculations, then copy back buffer } Is there a more CPU usage-efficient way of doing this? I know there''s a timer message but I''m not very familiar with it.

Share this post


Link to post
Share on other sites
Advertisement
First, you shouldn''t generally be locking the framerate. Implementing framerate-independent motion is almost always better than just locking the framerate. That said, use Sleep:

  
static DWORD TimeNow = timeGetTime();
DWORD TimeNext = timeGetTime();
if (TimeNext-TimeNow > FrameTimeDifference)
{
TimeNow = timeGetTime();
Sleep(TimeNext-TimeNow);
}

You should also be using multimedia timer to get more accurate timing. Also see timeBeginPeriod/timeEndPeriod.

Share this post


Link to post
Share on other sites
maybe it''s not the code you gave that makes your app eat 100% of your system resource.

i''ve experienced the same thing, my window application used the whole cpu usage. but, it''s not a directx application. i found what caused it, and it''s the message loop:

while (GetMessage(&msg, hwnd, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

that''s what i wrote, but as you can see, it''s supposed to be:

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

i don''t really remember the arguments of GetMessage(), but you notice the hwnd and NULL (assume hwnd is a valid window handle). although the application works fine in responding to the message, but it uses 100% cpu usage when it''s shutdown. i don''t know if you''re experiencing the same problem as i did, but i hope this helps.

Share this post


Link to post
Share on other sites
Hey, you may be satisfied, but I figure I''d chime in with the cause of your problem.

nicho_tedja is correct. It is the GetMessage function vs, PeekMessage. GetMessage will let only return when a message has been recieved. This is useful for apps, not games. PeekMessage will check periodically for a message (whenever it is called) and return regardless. If a message is recieved, then it will process it and carry on. If no message is recieved, it allow you (the user) to do something in your "spare time". Render, AI, etc. This will take up 100% of the CPU if nothing else is running. However, if you are running another app, you will notice that it may be split differently.

Depending on what you wish to accomplish, sleep should probably not be used. If you are trying to capture input and the like, you will be out of luck. Your controls will seem jittery. However, since this works for you, I''m wasting time...

-tim

Share this post


Link to post
Share on other sites
quote:
Original post by JovialPeon
If you are trying to capture input and the like, you will be out of luck. Your controls will seem jittery.


Not if he''s using buffered input and empties the input buffer on each game loop iteration.

Share this post


Link to post
Share on other sites
quote:
Original post by JovialPeon
nicho_tedja is correct. It is the GetMessage function vs, PeekMessage. GetMessage will let only return when a message has been recieved. This is useful for apps, not games. PeekMessage will check periodically for a message (whenever it is called) and return regardless. If a message is recieved, then it will process it and carry on. If no message is recieved, it allow you (the user) to do something in your "spare time".



Thanks for your insightful reply. I am using the PeekMessage function, as well as the GetMessage function. My message processing looks similar to the following:

if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage(&msg, NULL, 0, 0)) return (0);

TranslateMessage(&msg);
DispatchMessage(&msg);
}

I''m assuming this does give me the added "spare time" as you were saying.

quote:

Depending on what you wish to accomplish, sleep should probably not be used. If you are trying to capture input and the like, you will be out of luck. Your controls will seem jittery.



The sleep function does make control seem slightly jittery if not buffered, but locking the framerate would do that in most cases anyway. Also, it wouldn''t make much difference considering the way I''m implementing the controls.

Share this post


Link to post
Share on other sites
use Sleep(0).
it just releases the time slice to other apps. Its really not very good to fix your frame rates. i rather just let the frame rate run as fast as it can..

Its my duty, to please that booty ! - John Shaft

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!