Reputation: 39
I need to write a class that will attach to certain windows processes, monitor and limit their cpu usage. Process priority changing would not help me at all so i really need to write a program that is basically similar to BES or ThreadMaster. So i need to make a class that's doing something like this (pseudo code):
public void cpuLimiter(pid)
{
ProcessHandle handle = attachToProcess(pid);
while (cpuLimiting)
{
if (handle.cpuUsage > 30%)
{
handle.sleep(100miliseconds);
}
sleep(10miliseconds);
}
closeHandle(pid);
}
I hope i made it clear what i want to accomplish, just i have no idea how. Every help is appreciated.
Upvotes: 3
Views: 1664
Reputation: 294467
See Job Objects, CreateJobObject, SetInformationJobObject
and JOBOBJECT_CPU_RATE_CONTROL_INFORMATION
, AssignProcessToJobObject
. Do not attempt to hand-craft your own in-house process throttling, let the component specifically designed for this task (ie. jobs) handle the task.
See Working example of CreateJobObject/SetInformationJobObject pinvoke in .net? for managed usage of the NT Job native API.
Upvotes: 8