Process.ExitCode() == 0

G

Guest

I am calling out to an executable using the System.Diagnostics.Process
methods and specifically attempting to trap for errors (at least trying to
learn how to trap for errors). I arranged the data to specifically error
when calling this executable.

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "intupld.exe";
process.StartInfo.Arguments = hupFile;
process.StartInfo.WorkingDirectory = hencePath;
process.Start();
if (process.ExitCode != 0)
{

}

However: my .ExitCode is ALWAYS 0, regardless of if an error occured using
this exectable. When I run the executable from a dos prompt: I am able to
see the errors using the same file.

How would I actually be able to trap and find the errors that have occured
while running this executable?

Thanks
Andy
 
G

Guest

Andy,
It is up to you to set the exit code inside the target executable's code. If
you do not,
it will always be zero.
You might also want to consider using the WaitForExit method as well.
Peter
 
J

Jon Skeet [C# MVP]

Andy said:
I am calling out to an executable using the System.Diagnostics.Process
methods and specifically attempting to trap for errors (at least trying to
learn how to trap for errors). I arranged the data to specifically error
when calling this executable.

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "intupld.exe";
process.StartInfo.Arguments = hupFile;
process.StartInfo.WorkingDirectory = hencePath;
process.Start();
if (process.ExitCode != 0)
{

}

However: my .ExitCode is ALWAYS 0, regardless of if an error occured using
this exectable. When I run the executable from a dos prompt: I am able to
see the errors using the same file.

But what was the process exit code in dos? I suspect it was 0 there
too.
How would I actually be able to trap and find the errors that have occured
while running this executable?

Well, you can redirect StdErr and StdOut from the process, read them in
your code, and then determine whether or not there was an error.
 
G

Guest

Jon:
Well, you can redirect StdErr and StdOut from the process, read them in
your code, and then determine whether or not there was an error.

When I run the following code though: my output and output2 variables are
both empty strings. Not sure why this should be the case. There should be
some value I would assume.

process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
output = process.StandardError.ReadToEnd();
output2 = process.StandardOutput.ReadToEnd();
process.WaitForExit();

Thanks
Andy
 
J

Jon Skeet [C# MVP]

Andy said:
When I run the following code though: my output and output2 variables are
both empty strings. Not sure why this should be the case. There should be
some value I would assume.

process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
output = process.StandardError.ReadToEnd();
output2 = process.StandardOutput.ReadToEnd();
process.WaitForExit();

Well, it depends on how the program is writing out its data. Have you
tried the above with a small test program that definitely *does* write
to standard output and standard error? (A simple C# program would do
just fine.)

Note that in general doing it like that in one thread is a bad idea, as
if there's a lot of output, it may block on reading the *error* stream,
waiting for some of the output data to be read (which it won't until
the program has finished, which is when the error stream would be
closed). You'd generally read the two streams in different threads.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top