CPU usage limiting feature provided by Windows Security Essentials in Win32 APP

In Windows 7, Windows Security Essentials provides a feature which limits CPU usage during scan to specified value(in %).
So it seems it is possible to limit CPU usage of a process in Windows 7.

I have written a Win32 application which will be used in Windows 7. The application utilizes more than 85% of the CPU.
How can I limit this CPU usage of the application programmatically in Win32?

>I have written a Win32 application which will be used in Windows 7. The

application utilizes more than 85% of the CPU.How can I limit this CPU
usage of the application programmatically in Win32?

If that’s on a multi-core system then perhaps you should limit the number
of threads in your process.

One possible solution is to assign your process to a job object that has a
CPU rate control limitation but I never tested if this actually works
(JOBOBJECT_CPU_RATE_CONTROL_INFORMATION).

Another idea is that you call GetProcessTimes on your process and calculate
the amount of time it has been running (kernel+user time) and divide that
with the time that elapsed since the process started, taking into account
the number of processors in the system. Then you can add the necessary
sleeps in your code until you are under your threshold.

btw I think it’s OT for this group.

//Daniel

Sorry, I meant CPU time deltas, divided by the interval over which you are
measuring. The job object option is only available on Win8 and greater. In
any case you can add sleep() to your code to make it consume less
CPU.

//Daniel

Hi Daniel,

Is there any solution for Windows 7 & below regarding this issue?

Or i have to implement the sleep() logic in that case.

Thanks in advance!

> Or i have to implement the sleep() logic in that case.

Surely yes.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

You can lower the priority of your threads. It will not allow you to control
CPU consumption but you will be less invasive in case there is competition,
if that is useful depends on what you really want.

//Daniel

>As a side note, does anyone know how if at all power states in the system would be affected differently >between a normal priority thread that sleeps versus a low priority thread that runs all of the time?

A low priority thread that runs all the time will still run all the time if there is no competition so it will not allow the processor on which it’s running to go into a sleep state. A thread that calls sleep allows the processor to go into a power saving state if there are no other threads to be scheduled on that processor.

//Daniel