Answered by:
Programmatically limit CPU Usage of a Process

Question
-
Hi,
I have created process Programmatically, but process takes 90% of my CPU usage and I want to restrict my process limit of CPU usage.
How I can do this?
My Code is mentioned below:
ProcessStartInfo oInfo = new ProcessStartInfo(ffmpegParams.ffmpegPath, Parameters);
oInfo.UseShellExecute = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardError = true;
Process proc = new Process();
proc.StartInfo = oInfo;
//proc.PriorityClass = ProcessPriorityClass.BelowNormal;
proc.Start();
- Edited by Ravi Kumar Kamboj Monday, May 20, 2013 1:27 PM Mention Code
Answers
-
Hi Ravi,
Based on your further conformation, would you like to try this similar thread: http://stackoverflow.com/questions/11357713/how-to-limit-ffmpeg-cpu-usage?rq=1
Process ffmpeg = new Process(); ffmpeg.StartInfo.UseShellExecute = false; ffmpeg.StartInfo.FileName = "..\ffmpeg.exe"; ffmpeg.StartInfo.Arguments = "-threads 2"; // <=== Add this line ffmpeg.StartInfo.CreateNoWindow = true; ffmpeg.Start();
You can't limit FFMpeg to a percentage of CPU use, but you can set the
-threads
parameter on your FFMpeg call, if you have 4 cores try set it to-threads 2
that should limit you to around 50% CPU.Another solution might be to lower the priority on your FFMpeg process, to something lower than your applications.
I hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Proposed as answer by Eyal Solnik Monday, May 20, 2013 9:46 PM
- Marked as answer by Mike Feng Monday, May 27, 2013 11:03 AM
All replies
-
Hello Ravi,
I'm not really sure if this can help you out.
http://stackoverflow.com/questions/4852062/limiting-process-memory-with-maxworkingsetEric
Failure is not the worst thing in the world. The very worst is not to try. Email Address : ericjohnadamos@gmail.com. http://ericjohnadamos.blogspot.com/
-
If you write more efficient code then you may be able to shift the bottleneck to IO which will result in a lower percentage of CPU being used. If your program does not do any IO then you could always put thread.sleep calls in your code to make it not do anything for periods of time. As another alternative you could just exit and then no CPU will be used.
That was tongue-in-cheek if you didn't realise.
Seriously, the CPU is used when your program is doing work. If your program does unnecessary work then you will use unnecessary CPU time. Do you use bubble sort instead of more efficient alternatives? Do you read entire tables from the database and then filter in memory to get a single record? Do you calculate all primes less than 1,000,000,000 when you are asked for all primes less than 1,000? If you are analysing the output of the LHC and it makes your workstation too busy then maybe you should move the code to a dedicated machine.
Or, did you want us to tell you about the secret 'make my code more efficient so that it runs in 1% of the time but still produces the correct output' system option?
Paul Linton
-
Thanks for reply.
But I am not writing code which utilize my CPU usage. Actually I am using an EXE and creating a process programmatically. Then when I execute my code, it takes around 94% of my CPU usage because of that process. So, I want to restrict my process limit of CPU usage.
My Code is mentioned below:
ProcessStartInfo oInfo = new ProcessStartInfo(ffmpegParams.ffmpegPath, Parameters);
oInfo.UseShellExecute = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardError = true;
Process proc = new Process();
proc.StartInfo = oInfo;
//proc.PriorityClass = ProcessPriorityClass.BelowNormal;
proc.Start();
But it is not working.
- Edited by Ravi Kumar Kamboj Monday, May 20, 2013 1:27 PM Remove Corrention from code
-
Hi Ravi,
Based on your further conformation, would you like to try this similar thread: http://stackoverflow.com/questions/11357713/how-to-limit-ffmpeg-cpu-usage?rq=1
Process ffmpeg = new Process(); ffmpeg.StartInfo.UseShellExecute = false; ffmpeg.StartInfo.FileName = "..\ffmpeg.exe"; ffmpeg.StartInfo.Arguments = "-threads 2"; // <=== Add this line ffmpeg.StartInfo.CreateNoWindow = true; ffmpeg.Start();
You can't limit FFMpeg to a percentage of CPU use, but you can set the
-threads
parameter on your FFMpeg call, if you have 4 cores try set it to-threads 2
that should limit you to around 50% CPU.Another solution might be to lower the priority on your FFMpeg process, to something lower than your applications.
I hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Proposed as answer by Eyal Solnik Monday, May 20, 2013 9:46 PM
- Marked as answer by Mike Feng Monday, May 27, 2013 11:03 AM