I want to create a program which can limit cpu usage of a process even when the computer is idle.
I have made a program that set priority of process, but if the computer is idle, the cpu usage can reach 95%. The process contains "element" is the process that I want to limit
private static readonly string[] RestrictedProcess = new[] { "element" };
static void ProcessChecker(object o)
{
List<Process> resProc = new List<Process>();
foreach(Process p in Process.GetProcesses())
{
string s = p.ProcessName;
foreach(string rp in RestrictedProcess)
{
s = s.ToLower();
if (s.Contains(rp))
resProc.Add(p);
}
}
foreach(Process p in resProc)
{
p.PriorityBoostEnabled = false;
p.PriorityClass = ProcessPriorityClass.Idle;
p.MaxWorkingSet = new IntPtr(20000000);
}
SetPowerConfig(resProc.Count > 0 ? PowerOption.GreenComputing : PowerOption.Balanced);
}
If the program you want to limit is not yours, there are several options:
Idleand do not limit the CPU usage as the CPU should be used as much as possible in any case. It’s OK to have your CPU running 100% all the time if there is something useful to do. If the priority isidle, then the CPU usage of this particular process will be reduced if another program requires CPU.To suspend/resume a process that is not yours, you’ll have to use P/Invoke (and this requires to have access to the process, so if you are Windows Vista or above, take care of UAC for admin rights):