469,323 Members | 1,553 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Process.ExitCode() == 0

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
Feb 20 '06 #1
4 30473
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Andy" wrote:
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

Feb 20 '06 #2
Andy <An**@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 20 '06 #3
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
Feb 20 '06 #4
Andy <An**@discussions.microsoft.com> wrote:
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();


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dan McGuffin | last post: by
5 posts views Thread by Stanley | last post: by
2 posts views Thread by Grigs | last post: by
4 posts views Thread by Christian Billig | last post: by
2 posts views Thread by =?Utf-8?B?U3RldmVU?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.