以低优先级启动进程。 不用try-catch也可以吗?

发布于 2024-07-25 08:56:53 字数 702 浏览 10 评论 0原文

不久前,我询问了当 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遇到 2024-08-01 08:56:53

您的选择是:

  1. 尝试设置它并捕获异常(如果它已经退出)。
  2. 首先检查进程是否已退出,然后尝试设置它......如果已经退出,仍然捕获异常。

因为即使您进行检查,该过程仍然可能在您检查和尝试设置之间退出。 所以,实际上,只需选择选项#1,因为它的代码更少。

Your choices are:

  1. Try to set it and catch the exception if it has already exited.
  2. Check to see if the process has exited first and then try to set it ... and still catch the exception if it has already exited.

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.

流绪微梦 2024-08-01 08:56:53

解决方案:启动挂起的进程,然后更改优先级,然后恢复该进程。

这是在 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.

对不⑦ 2024-08-01 08:56:53

为什么您想要避免您知道如何在尽可能最低的级别处理的捕获和异常情况? 如果您可以设置优先级,那就太好了! 如果没有,该过程将退出并且您的工作已完成。 我认为没有任何理由尝试避免适当处理错误。

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.

暗恋未遂 2024-08-01 08:56:53

你能做的任何测试都将立即成为竞争条件; 如果应用程序快速退出,我能想到的唯一安全的方法就是使用 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.

哭泣的笑容 2024-08-01 08:56:53

您始终可以使用 if 语句:

if (!app.HasExited)
{
   app.PriorityClass = ProcessPriorityClass.BelowNormal;
}

尽管我同意其他答案,但您仍然应该捕获异常并正确处理它们。

You could always use an if statement:

if (!app.HasExited)
{
   app.PriorityClass = ProcessPriorityClass.BelowNormal;
}

I agree with the other answers though, you should still be catching your exceptions and handling them properly anyways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文