如何启动具有空闲优先级的 .NET 进程?
我正在使用 System.Diagnostics.ProcessStartInfo 设置用于从 .NET 程序启动进程的参数。一旦进程启动,我可以使用
myProcess.PriorityClass = ProcessPriorityClass.Idle
将进程的优先级更改为空闲,以便它只在后台运行,并且不会占用我的 CPU 功率。有没有办法使用 ProcessStartInfo 对象来指定进程应以“空闲”优先级启动,以便在执行期间进程的运行速度不会高于空闲速度?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Process 类中没有提供 API 来启动具有不同优先级的进程。最好的选择是在启动该过程后立即设置它。启动该流程后,您可以设置流程。 PriorityClass:
如果您希望阻止进程以更高的优先级运行,darin 的回答提供了使用 P/Invoke 和 Windows API 的解决方法。即使这会以正常优先级启动进程,但如果它在挂起状态下启动,它将不会运行,因此优先级将不起作用。
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:
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.
启动挂起的进程,然后更改优先级,然后恢复该进程。您可以使用 CreateProcess Win32 函数执行此操作CREATE_SUSPENDED 标志,但不幸的是我不确定是否有.NET 对此提供支持,您可能需要求助于 P/调用。
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.