3

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.

6
  • 4
    beg you for the most complete code This kind of makes it sound like you want us to just spoon-feed you a solution rather than addressing a specific problem with code you've written. The latter is a good question, the former is not. Please show what you've tried. Commented Aug 18, 2014 at 14:28
  • It is definitely going to be harder than what you think. Can't be answered in StackOverflow answer. Try something, run into a specific problem. Come here. We're happy to help. Commented Aug 18, 2014 at 14:31
  • 1
    possible duplicate of How can I programmatically limit my program's CPU usage to below 70%? Commented Aug 18, 2014 at 14:32
  • tnw thanks for your remark, i agree with you, actually i didnt ask for a complete solution, i was just afraid if someone sends me to some assembly stuff, and i know i wouldnt even understand it Commented Aug 18, 2014 at 14:49
  • Is there any way to just send sleep(x miliseconds) to another process in windows? everything else i can improvise or i have already done, but i have no idea at all how to send sleep or suspend / start to another process in windows. Commented Aug 18, 2014 at 14:52

1 Answer 1

8
  • You create a JOB object
  • You limit the CPU for the JOB
  • you attach the target process to the JOB

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch for your answer, it will take me lots of time to understand this so im gonna get on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.