How to find out when .Net System.Diagnostics.Process worked successfully or failed?

I write a planner or sort. This is basically a table with a list of exes (for example, "C: \ a.exe") and a console application that scans records in the table every minute and runs tasks that have not yet been completed.

I run the following tasks:

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

How can I find out if a specific task failed? For example, what if a.exe throws an unhandled exception? I would like the code above to know when this will happen, and update the task table with something like “a specific task failed”, etc.

How can i do this?

I do not use Sql Agent or Windows Scheduler because someone did not tell me. He has more “experience”, so I basically follow orders. Feel free to suggest alternatives.

+5
source share
4 answers

You can catch Win32Exception to check Process.Start () failed due to file failure or lack of access.

But you cannot catch the exceptions thrown by the processes that you create with this class. First, the application may not be written in .NET, so there can be no concept of exception at all.

, ExitCode StandardOutput StandardError, , .

+8

, Process.ExitCode, , . , WaitForExit(). ErrorDataReceived, stderr.

+5

ExitCode - :

string output = p.StandardOutput.ReadToEnd();

, . , .

+2

, @jop. . :

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

Nonzero codes are usually errors. Some applications use negative ones, these are errors, and positive ones as status / warning codes.

0
source

All Articles