views:

311

answers:

2

I'm using System.Diagnostics.ProcessStartInfo to set up the parameters for launching a process from a .Net program. Once the the process is started, I can use

myProcess.PriorityClass = ProcessPriorityClass.Idle

to change the priority for the process to idle, so that it only runs in the background, and doesn't hog my CPU power. Is there any way using the ProcessStartInfo object to specify that the process should start with an "Idle" priority, so that at no time during execution is the process running at higher than idle speed?

A: 

There is no provided API in the Process class to start a process with a different priority. The best option is to just set it immediately after starting the process. Once you start the process, you can set the Process.PriorityClass:

var myProcess = Process.Start(...);
myProcess.PriorityClass = ProcessPriorityClass.Idle;

If you wish to prevent the process from running with a higher priority, darin's answer provides a workaround using P/Invoke and the Windows API. Even this starts the process with a normal priority, but if it's started in a suspended state, it will not run, so the priority would have no effect.

Reed Copsey
I already stated in the question that I am doing this. However, I want to set the priority before the process starts running. Please read the question.
Kibbee
Why the downvote? You can't set the priority in ProcessStartInfo, but you CAN set it on a process you've created, which is what I was showing...
Reed Copsey
You can do this immediately after creating the process, but not before. I'm sorry, but that IS the answer - there is no other alternative.
Reed Copsey
I specifically stated that I wanted to set the priority "before" the process starts to run, so that at no time the process is running at higher than idle speed.
Kibbee
Then look to darin's answer - there is no managed API for this, using the process class. The process class doesn't provide a means for doing it. I'll edit my answer to be more specific.
Reed Copsey
+1  A: 

Start the process suspended, then change the priority, and then resume the process. You do this with the CreateProcess Win32 function using the CREATE_SUSPENDED flag but unfortunately I am not sure if there's support in .NET for this and you might need to resort to P/Invoke.

Darin Dimitrov