Jump to content
  • Advertisement
Sign in to follow this  
ThunderSoul

[.net] CPU workout

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

Hello all... ^_^ I try rendering my scene by either using a while loop or using "this.Invalidate()" within the OnPaint statement... (I'm using C#)... but these 2 methods use up the entire CPU since they're both similar to a "while (true)" statement... Is there a way to minimize that CPU consumption, I don't see any 3D games I play using barely any CPU power at all... Thank you...

Share this post


Link to post
Share on other sites
Advertisement
I believe a simple sleep call should fix that.
while(isRunning)
{
runGame();
drawGame();

Sleep(1);
}

Use google to find the corresponding call in C#

Share this post


Link to post
Share on other sites
Another option would be to limit your framerate to your display's refresh rate (vsyncing, using PresentParameters.PresentationInterval = PresentInterval.One). This way calling Device.Present() will block/sleep the rendering thread for as long as needed to match the specified framerate and free up system CPU time this way.

This is probably preferable over a Thread.Sleep, since the Sleep call will pause the CPU blindly and thus slows down your rendering thread. Using a fixed presentation interval adapts the 'pause time' to the current time it takes to actually render a frame. So as you add more CPU heavy code, the pause time will go down to maintain the target framerate.

Hope this helps and/or makes sense :)

Share this post


Link to post
Share on other sites
Quote:
Original post by ThunderSoul
I don't see any 3D games I play using barely any CPU power at all...

I don't know what games you've been playing, but all of mine seem to use up as much CPU as they can get their hands on. There's nothing wrong with an exclusive-mode game keeping the CPU busy. In fact, it's the only way to guarantee optimal performance.

Admiral

Share this post


Link to post
Share on other sites
Quote:
Original post by blueapple
Doesn't a Sleep(1)...or is it Sleep(0) call just give up the current unused CPU time?

Quick search on these forums


Sleep(0) gives up the rest of the current CPU time slice allocated for your process. Sleep(1) will attempt to sleep for 1 millisecond, but it'd probably sleep for more like 10 milliseconds due to low timer granularity.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sc4Freak
Quote:
Original post by blueapple
Doesn't a Sleep(1)...or is it Sleep(0) call just give up the current unused CPU time?

Quick search on these forums


Sleep(0) gives up the rest of the current CPU time slice allocated for your process. Sleep(1) will attempt to sleep for 1 millisecond, but it'd probably sleep for more like 10 milliseconds due to low timer granularity.
Use of Sleep() in time-sensitive applications (e.g. multimedia) isn't a good idea as you've no guarantee as to when your application will regain control. The parameter is stating a minimum delay.

I've not experienced it myself, but I've read accounts of people who traced user-input "bugs" to Sleep() calls - silly things like jittering mice cursors, irregular typing speeds and so on all due to Sleep() taking a non-deterministic time to return.

hth
Jack

P.S. This doesn't seem to be DirectX related, so I'm bumping this over to the .NET forum...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!