PDA

View Full Version : CPU usage when app not active


shadwdrak
07-01-2004, 05:10 PM
I noticed that the CPU usage was spiking to 100% when the app was not active. Normally this is if its a windowed app, or if you alt-tabbed out of a full screen app.

I fixed this by having the framework sleep 5 seconds in the main loop if it wasn't active.

look at the code below, I added an else, and the Sleep inside it.

void Core::Loop()
{

...

if( mbActive )
{
...
} // active
else
{
Sleep(5); //if we are not the active app, then be nice and free up the cpu.
}
...
}

bladder
07-01-2004, 09:20 PM
yeah, that should have been done before. Always good to let the CPU have some time to itself when inactive.

Bored_Now
01-27-2005, 12:04 PM
I noticed that the CPU usage was spiking to 100% when the app was not active. Normally this is if its a windowed app, or if you alt-tabbed out of a full screen app.

I fixed this by having the framework sleep 5 seconds in the main loop if it wasn't active.

look at the code below, I added an else, and the Sleep inside it.

void Core::Loop()
{

...

if( mbActive )
{
...
} // active
else
{
Sleep(5); //if we are not the active app, then be nice and free up the cpu.
}
...
}
8547


I've had a think about this and would like to suggest an alternative. The active status should only change when a windows message is received. Ideally the game should respond as soon as this is received. A way to do this is to replace the Sleep call with a GetMessage() / TranslateMessage() / DispatchMessage() set of calls. The GetMessage() call will block until a message is received and so free the CPU until the game needs it again.