Catch process exception

E

Eran.Yasso

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?
 
E

Eran.Yasso

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. :(
 
S

sloan

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();
}
}


}
 
E

Eran.Yasso

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();

}
}
}






- 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();
 
B

Ben Voigt

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
 
E

Eran.Yasso

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?
 
E

Eran.Yasso

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.
 
B

Ben Voigt

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.)
 

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