[.net] CPU workout

Started by
6 comments, last by benryves 16 years, 10 months ago
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...
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#
Check out my devlog.
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 :)
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Doesn't a Sleep(1)...or is it Sleep(0) call just give up the current unused CPU time?

Quick search on these forums
Check out my devlog.
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
Ring3 Circus - Diary of a programmer, journal of a hacker.
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.
NextWar: The Quest for Earth available now for Windows Phone 7.
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...

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

The usual way I've seen is to catch the Application.Idle event. Example on Tom Miller's Blog.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement