[.net] CPU workout
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...
I believe a simple sleep call should fix that.
Use google to find the corresponding call in C#
while(isRunning){ runGame(); drawGame(); Sleep(1);}
Use google to find the corresponding call in C#
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 :)
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 :)
Doesn't a Sleep(1)...or is it Sleep(0) call just give up the current unused CPU time?
Quick search on these forums
Quick search on these forums
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
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.
Quote:Original post by Sc4FreakUse 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.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.
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...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement