以低优先级启动进程。 不用try-catch也可以吗?
不久前,我询问了当 app.PriorityClass = ? 时发生的错误消息。 在 app.start 之前。 显然我不喜欢的解决方案是在启动后编写它。
直到今天,它都没有太大问题。 我收到“无法处理请求,因为进程已退出”。 异常,因为进程完成得足够快,不会改变其优先级(有时)。 尝试解决这个问题感觉很糟糕。 真正的解决办法是什么? 如何启动低优先级进程?
Process app = new Process();
app.StartInfo.FileName = @"bin\convert.exe";
app.StartInfo.Arguments = string.Format("{0} -resize 150x150 {1}", filename, thumbName);
//app.PriorityClass = ProcessPriorityClass.BelowNormal; //No process is associated with this object.
app.Start();
//app.PriorityClass = ProcessPriorityClass.BelowNormal; //"Cannot process request because the process has exited."
Not to long ago i asked about an error msg that occurred when when app.PriorityClass = ? was before app.start. Apparently the solution which i didnt like was to write it after start.
It worked without much problem until today. I get a "Cannot process request because the process has exited." exception because the process completes quickly enough to not have its priority changed (some of the time). Wrapping a try around this feels bad. What is the real solution? how do i launch a process with low priority ?
Process app = new Process();
app.StartInfo.FileName = @"bin\convert.exe";
app.StartInfo.Arguments = string.Format("{0} -resize 150x150 {1}", filename, thumbName);
//app.PriorityClass = ProcessPriorityClass.BelowNormal; //No process is associated with this object.
app.Start();
//app.PriorityClass = ProcessPriorityClass.BelowNormal; //"Cannot process request because the process has exited."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的选择是:
因为即使您进行检查,该过程仍然可能在您检查和尝试设置之间退出。 所以,实际上,只需选择选项#1,因为它的代码更少。
Your choices are:
Because even when you check, the process could still exit between when you check and when you try to set it. So, really, just go with option #1 because it's less code.
解决方案:启动挂起的进程,然后更改优先级,然后恢复该进程。
这是在 Win32 中通过调用 CreateProcess 中的 CREATE_SUSPENDED 完成的,但不幸的是我不知道 .NET 的方式。
The solution: start the process suspended, then change the priority, and then resume the process.
This is done in Win32 with CREATE_SUSPENDED in the call to CreateProcess, but unfortunately I don't know the .NET way offhand.
为什么您想要避免您知道如何在尽可能最低的级别处理的捕获和异常情况? 如果您可以设置优先级,那就太好了! 如果没有,该过程将退出并且您的工作已完成。 我认为没有任何理由尝试避免适当处理错误。
Why would you want to avoid catching and exception condition that you know how to deal with at the lowest possible level? If you can set the priority, great! If not, the process has quit and your work is done. I don't think there's any reason to try and avoid appropriately handling the error.
你能做的任何测试都将立即成为竞争条件; 如果应用程序快速退出,我能想到的唯一安全的方法就是使用
try
/catch
。除非它从 stdio 读取? 在这种情况下,您可以重定向它并故意不关闭输入流,直到更改优先级。
Any test you can do is going to immediately be a race condition; if the app exits quickly, the only safe thing I can think of is to use
try
/catch
.Unless it reads from stdio? In which case you could redirect it and deliberately not close the input stream until you've changed the priority.
您始终可以使用 if 语句:
尽管我同意其他答案,但您仍然应该捕获异常并正确处理它们。
You could always use an if statement:
I agree with the other answers though, you should still be catching your exceptions and handling them properly anyways.