469,327 Members | 1,295 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Catch process exception

Hi,

My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?

In both ways, the exit code of this process is zero.

I tried using the following, but it goes to catch.

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());
}
catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
}
when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.

Is there any other way or am I doing any thing wrong?

Feb 26 '07 #1
9 18188
On Feb 26, 4:42 pm, Eran.Ya...@gmail.com wrote:
Hi,

My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?

In both ways, the exit code of this process is zero.

I tried using the following, but it goes to catch.

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());}

catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;}

when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.

Is there any other way or am I doing any thing wrong?
Ooops, forgot. Thanks for the help. :(

Feb 26 '07 #2

You need to stop mixing "API , was it Zero or something else" mentality with
Exception Handling.
Below is a stab at a rewrite/refactor.

Handle the exception ( a specific exception like IOException and not
"Exception" on the outside calling code.


private string RunTheProcess(SomeObject cfrmTRSobj)
{
StreamReader myStreamReader = null;
ProcessStartInfo myProcessStartInfo = null;
try
{
myProcessStartInfo = new ProcessStartInfo();

myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError; //HUH???
return myStreamReader.ReadToEnd();

}

// purposely, there is no catch statement here
finally
{
//clean up here
if(null!=myStreamReader)
{
myStreamReader.Close();
}
}
}

<Er********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
On Feb 26, 4:42 pm, Eran.Ya...@gmail.com wrote:
Hi,

My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?

In both ways, the exit code of this process is zero.

I tried using the following, but it goes to catch.

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());}

catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;}

when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.

Is there any other way or am I doing any thing wrong?

Ooops, forgot. Thanks for the help. :(

Feb 26 '07 #3
On Feb 26, 5:31 pm, "sloan" <s...@ipass.netwrote:
You need to stop mixing "API , was it Zero or something else" mentality with
Exception Handling.

Below is a stab at a rewrite/refactor.

Handle the exception ( a specific exception like IOException and not
"Exception" on the outside calling code.

private string RunTheProcess(SomeObject cfrmTRSobj)
{

StreamReader myStreamReader = null;
ProcessStartInfo myProcessStartInfo = null;
try
{

myProcessStartInfo = new ProcessStartInfo();

myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError; //HUH???
return myStreamReader.ReadToEnd();

}

// purposely, there is no catch statement here

finally
{
//clean up here
if(null!=myStreamReader)
{
myStreamReader.Close();

}
}
}
<Eran.Ya...@gmail.comwrote in message

news:11**********************@q2g2000cwa.googlegro ups.com...
On Feb 26, 4:42 pm, Eran.Ya...@gmail.com wrote:
Hi,
My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?
In both ways, the exit code of this process is zero.
I tried using the following, but it goes to catch.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());}
catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;}
when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.
Is there any other way or am I doing any thing wrong?
Ooops, forgot. Thanks for the help. :(- Hide quoted text -

- Show quoted text -
Hi, sloan.

I took the sample from MSDN.
Process myProcess = new Process();

//I choose not to send parameters to the constructor
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("net ","use
"+ args[0]);

myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

StreamReader myStreamReader = myProcess.StandardError; //<------HUH???
// Read the standard error of net.exe and write it on to console.
Console.WriteLine( myStreamReader.ReadLine());
myProcess.Close();
Feb 26 '07 #4

<Er********@gmail.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
Hi,

My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?

In both ways, the exit code of this process is zero.

I tried using the following, but it goes to catch.

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());
}
catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
}
when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.

Is there any other way or am I doing any thing wrong?
From the documentation:

p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected error stream.
// p.WaitForExit();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
The code example avoids a deadlock condition by calling
p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
result if the parent process calls p.WaitForExit before
p.StandardError.ReadToEnd

Feb 26 '07 #5
On Feb 26, 7:12 pm, "Ben Voigt" <r...@nospam.nospamwrote:
<Eran.Ya...@gmail.comwrote in message

news:11**********************@s48g2000cws.googlegr oups.com...


Hi,
My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?
In both ways, the exit code of this process is zero.
I tried using the following, but it goes to catch.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());
}
catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
}
when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.
Is there any other way or am I doing any thing wrong?

From the documentation:

p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected error stream.
// p.WaitForExit();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
The code example avoids a deadlock condition by calling
p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
result if the parent process calls p.WaitForExit before
p.StandardError.ReadToEnd- Hide quoted text -

- Show quoted text -
Can you send me a link to that please?

Feb 26 '07 #6
On Feb 26, 7:12 pm, "Ben Voigt" <r...@nospam.nospamwrote:
<Eran.Ya...@gmail.comwrote in message

news:11**********************@s48g2000cws.googlegr oups.com...


Hi,
My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?
In both ways, the exit code of this process is zero.
I tried using the following, but it goes to catch.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());
}
catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
}
when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.
Is there any other way or am I doing any thing wrong?

From the documentation:

p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected error stream.
// p.WaitForExit();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
The code example avoids a deadlock condition by calling
p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
result if the parent process calls p.WaitForExit before
p.StandardError.ReadToEnd- Hide quoted text -

- Show quoted text -
Hi,

Can you send me link please?

thanks.

Feb 26 '07 #7

<Er********@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
On Feb 26, 7:12 pm, "Ben Voigt" <r...@nospam.nospamwrote:
><Eran.Ya...@gmail.comwrote in message

news:11**********************@s48g2000cws.googleg roups.com...


Hi,
My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?
In both ways, the exit code of this process is zero.
I tried using the following, but it goes to catch.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());
}
catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
}
when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.
Is there any other way or am I doing any thing wrong?

From the documentation:

p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected error stream.
// p.WaitForExit();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
The code example avoids a deadlock condition by calling
p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
result if the parent process calls p.WaitForExit before
p.StandardError.ReadToEnd- Hide quoted text -

- Show quoted text -

Can you send me a link to that please?
http://msdn2.microsoft.com/en-us/lib...darderror.aspx
Feb 26 '07 #8
On Feb 26, 11:15 pm, "Ben Voigt" <r...@nospam.nospamwrote:
<Eran.Ya...@gmail.comwrote in message

news:11**********************@j27g2000cwj.googlegr oups.com...


On Feb 26, 7:12 pm, "Ben Voigt" <r...@nospam.nospamwrote:
<Eran.Ya...@gmail.comwrote in message
>news:11**********************@s48g2000cws.googleg roups.com...
Hi,
My app starts process. Some times this process exits because of
exception. Can my app know if the process exited due to exception or
gracefully?
In both ways, the exit code of this process is zero.
I tried using the following, but it goes to catch.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
try
{
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
TestProcess.StartInfo = myProcessStartInfo;
TestProcess = Process.Start( MyProcess , " -file "+
cfrmTRSobj.GetTRSFullFileName() );
SetProcID( TestProcess );
TestProcess.WaitForExit();
StreamReader myStreamReader = TestProcess.StandardError;
MessageBox.Show(myStreamReader.ReadToEnd());
}
catch( Exception e )
{
bRetval = frmMain.RETVALSTATUS.FAIL;
TestStatus = frmMain.DGETestStatusenums.STATUSFAILED;
}
when the app reached to "StreamReader myStreamReader =
TestProcess.StandardError;" it jump to catch.
Is there any other way or am I doing any thing wrong?
From the documentation:
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected error stream.
// p.WaitForExit();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
The code example avoids a deadlock condition by calling
p.StandardError.ReadToEnd before p.WaitForExit. A deadlock condition can
result if the parent process calls p.WaitForExit before
p.StandardError.ReadToEnd- Hide quoted text -
- Show quoted text -
Can you send me a link to that please?

http://msdn2.microsoft.com/en-us/lib....process.s...- Hide quoted text -

- Show quoted text -
Thanks, but can I use this method to get ending reason of
app(exception,user ending, etc...).

Feb 27 '07 #9
>
Thanks, but can I use this method to get ending reason of
app(exception,user ending, etc...).
From the code you provided, I was under the impression you were trying to
capture an error message the app writes to stderr. Is that accurate?

According to the documentation for GetExitCodeProcess, an application that
ends with an unhandled exception should terminate, and the exit code should
be the exception. But maybe the exception isn't unhandled, and the
application catches it and exits normally... in that case your only option
is to check for any error event the application might leave (could be a log
file, Windows Event Log, message on stdout or stderr, etc.)
Feb 27 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Joe Rigley | last post: by
6 posts views Thread by Tilfried Weissenberger | last post: by
37 posts views Thread by clintonG | last post: by
8 posts views Thread by CarpetMnuncher! | last post: by
4 posts views Thread by K Viltersten | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.