473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 18819
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Joe Rigley | last post by:
Hello, As a .NET newbie I'd appreciate some advice... I've been tasked with writing a small app (in VB .NET) that takes some data from our HR system, does some format modifications, and loads...
6
by: Tilfried Weissenberger | last post by:
Hi, I am a bit confused as to what the FINALLY block is meant for. What's the difference between: this.Cursor = Cursors.WaitCursor; try { //do some stuff } catch { //handle exception }...
1
by: TD | last post by:
I'm using iText in an app to create and print documents to network printers. I launch acrobat via the command line as shown on this page... ...
37
by: clintonG | last post by:
Has somebody written any guidelines regarding how to determine when try-catch blocks should be used and where their use would or could be considered superfluous? <%= Clinton Gallagher...
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
8
by: CarpetMnuncher! | last post by:
================================================================= How do I use Try Catch error handling when a timer is involved? If I preform the preciduer below and I get an error I revive...
14
by: libs | last post by:
I have A.exe that should catch the return code of B.exe (both are written in VB.net) so A.exe can continue processing other commands. but A.exe cannot catch B's return code so an exception is not...
34
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
4
by: K Viltersten | last post by:
I've developed a service and protected it's contents from presenting the potential error messages by a number of try-catch statements, especially a general, top level try-catch. When i run it,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.