如何启动低优先级进程? C#
我想执行命令行工具来处理数据。 它不需要阻塞。 我希望它的优先级较低。 所以我写了下面的内容
Process app = new Process();
app.StartInfo.FileName = @"bin\convert.exe";
app.StartInfo.Arguments = TheArgs;
app.PriorityClass = ProcessPriorityClass.BelowNormal;
app.Start();
但是,我收到一个 System.InvalidOperationException
,其中包含消息“没有进程与此对象关联”。 为什么? 如何以低优先级正确启动此应用程序?
如果没有 app.PriorityClass = ProcessPriorityClass.BelowNormal;
行,应用程序运行正常。
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." Why? How do I properly launch this app in low priority?
Without the line app.PriorityClass = ProcessPriorityClass.BelowNormal;
the app runs fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在启动该过程后设置 PriorityClass。 任务管理器以这种方式工作,允许您为已经运行的进程设置优先级。
Try setting the PriorityClass AFTER you start the process. Task Manager works this way, allowing you to set priority on a process that is already running.
您可以通过一些小技巧来创建一个优先级较低的进程。 您降低父进程优先级,创建新进程,然后恢复到原始进程优先级:
child
将在开始时具有Idle
进程优先级。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:
child
will have theIdle
process priority right at the start.如果您准备 P/Invoke
CreateProcess
,则可以在标志中传递CREATE_SUSPENDED
。 然后您可以在恢复进程之前调整进程优先级。If you're prepared to P/Invoke to
CreateProcess
, you can passCREATE_SUSPENDED
in the flags. Then you can tweak the process priority before resuming the process.