How to limit CPU usage in Windows?

I need to control the use of my application processor to a certain limit. My application will work in Win XP, Vista, Win7 and Windows 8.

I tried to implement to get the current CPU usage of the processor and use the Sleep () method. (I used the GetProcessTimes & GetSystemTimes API)

pseudo code:

for(;;) { //Get the current process CPU Usage int cpuUsage = CalculateCPUUsage(); if(cpuUsage > 50) Sleep(10) else { //Project implementation code } } 

Question:

Can I write an application to monitor CPU usage of the processor and whenever the CPU reaches an acceptable limit, stop the process and continue it.

Thanks in advance for your help.

+4
source share
2 answers

You can limit the CPU usage to your process or any other process by adding interest to the Job object and placing restrictions on the Job object.

One of the resource limitations that can be configured for Job objects is CPU utilization:

If you need to use the approach before Windows 8, pay attention to the note:

To register for a notification when this limit is exceeded without terminating processes, use the SetInformationJobObject function with the JobObjectNotificationLimitInformation information class

+1
source

If you are just trying to give a process a lower priority to be more enjoyable for other threads, you can set its priority with SetThreadPriority, for example:

 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL); 
+1
source

Source: https://habr.com/ru/post/1440868/


All Articles