简体   繁体   English

如何判断.Net System.Diagnostics.Process成功运行或失败?

[英]How can I tell when .Net System.Diagnostics.Process ran successfully or failed?

I'm writing a scheduler or sorts. 我正在编写调度程序或排序。 It's basically a table with a list of exes (like "C:\\a.exe") and a console app that looks at the records in the table every minute or so and runs the tasks that haven't been run yet. 它基本上是一个包含exes列表的表(如“C:\\ a.exe”)和一个控制台应用程序,它每分钟左右查看表中的记录并运行尚未运行的任务。

I run the tasks like this: 我运行这样的任务:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = someExe; // like "a.exe"
p.Start();

How can I tell if a particular task failed? 如何判断特定任务是否失败? For example what if a.exe throws an unhandled exception? 例如,如果a.exe抛出未处理的异常怎么办? I'd like the above code to know when this happens and update the tasks table with something like "the particular task failed" etc. 我想让上面的代码知道这种情况何时发生,并用“特定任务失败”等内容更新任务表。

How can I do this? 我怎样才能做到这一点?

I'm not using the Sql Agent or the Windows Scheduler because someone else told me not to. 我没有使用Sql Agent或Windows Scheduler,因为其他人告诉我不要。 He has more "experience" so I'm basically just following orders. 他有更多的“经验”所以我基本上只是遵循命令。 Feel free to suggest alternatives. 随意提出替代方案。

You can catch the Win32Exception to check if Process.Start() failed due to the file not existing or execute access is denied. 您可以捕获Win32Exception以检查Process.Start()是否由于文件不存在而失败,或者执行访问被拒绝。

But you can not catch exceptions thrown by the processes that you create using this class. 但是您无法捕获使用此类创建的进程抛出的异常。 In the first place, the application might not be written in .NET so there might not be a concept of exception at all. 首先,应用程序可能不是用.NET编写的,因此可能根本没有异常的概念。

What you can do is check on the ExitCode of the application or read the StandardOutput and StandardError streams to check whether error messages are being posted. 您可以做的是检查应用程序的ExitCode或读取StandardOutputStandardError流以检查是否正在发布错误消息。

I think you're looking for Process.ExitCode, assuming the process returns one. 我认为你正在寻找Process.ExitCode,假设进程返回一个。 You may need to use WaitForExit() though. 您可能需要使用WaitForExit()。 There is also an ErrorDataReceived event which is triggered when an app sends to stderr. 还有一个ErrorDataReceived事件,当应用程序发送到stderr时会触发该事件。

In addition to the ExitCode, you can also do something like this: 除了ExitCode之外,您还可以执行以下操作:

string output = p.StandardOutput.ReadToEnd();

That will capture everything that would have been written to a command window. 这将捕获本应写入命令窗口的所有内容。 Then you can parse that string for known patterns for displaying errors, depending on the app. 然后,您可以解析该字符串以查找已知模式以显示错误,具体取决于应用程序。

To expand on what @jop said. 扩展@jop所说的内容。 You will also need to wait for the process to close. 您还需要等待该过程关闭。 Thus: 从而:

        p.Start();
        p.WaitForExit();
        int returnCode = p.ExitCode;

Non-zero codes are typically errors. 非零代码通常是错误。 Some application use negative ones are errors, and positive ones as status codes/warnings. 一些应用程序使用否定的是错误,而正面的应用程序使用状态代码/警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM