[英]How do I launch a process with low priority? C#
I want to execute a command line tool to process data.我想执行一个命令行工具来处理数据。 It does not need to be blocking.
它不需要阻塞。 I want it to be low priority.
我希望它是低优先级的。 So I wrote the below
所以我写了下面
Process app = new Process();
app.StartInfo.FileName = @"bin\convert.exe";
app.StartInfo.Arguments = TheArgs;
app.PriorityClass = ProcessPriorityClass.BelowNormal;
app.Start();
However, I get a System.InvalidOperationException
with the message "No process is associated with this object."但是,我收到
System.InvalidOperationException
消息“没有进程与此 object 关联”。 Why?为什么? How do I properly launch this app in low priority?
如何以低优先级正确启动此应用程序?
Without the line app.PriorityClass = ProcessPriorityClass.BelowNormal;
没有行
app.PriorityClass = ProcessPriorityClass.BelowNormal;
the app runs fine.该应用程序运行良好。
Try setting the PriorityClass AFTER you start the process.尝试在启动该过程后设置 PriorityClass。 Task Manager works this way, allowing you to set priority on a process that is already running.
任务管理器以这种方式工作,允许您为已经运行的进程设置优先级。
You can create a process with lower priority by doing a little hack.您可以通过一些技巧来创建优先级较低的进程。 You lower the parent process priority, create the new process and then revert back to the original process priority:
您降低父进程优先级,创建新进程,然后恢复为原始进程优先级:
var parent = Process.GetCurrentProcess();
var original = parent.PriorityClass;
parent.PriorityClass = ProcessPriorityClass.Idle;
var child = Process.Start("cmd.exe");
parent.PriorityClass = original;
child
will have the Idle
process priority right at the start. child
将在开始时具有Idle
进程优先级。
If you're prepared to P/Invoke to CreateProcess
, you can pass CREATE_SUSPENDED
in the flags.如果您准备 P/Invoke 到
CreateProcess
,则可以在标志中传递CREATE_SUSPENDED
。 Then you can tweak the process priority before resuming the process.然后您可以在恢复进程之前调整进程优先级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.