Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bubble up exception back to caller when using Process.Start()

I have a windows form application and a command prompt exe. I'm executing the command prompt whithin win form button click

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "ConsoleApplication2.exe";
Process.Start(info);

And i have some operation whtin button click event which is executed after calling the exe. But i will only need to perform if there is no exception thrown from command prompt exe. Basically what is need is to bubble up the exception from exe to win from. I tried throwing the exception but since both exe and win from are two different application instances exception is not thrown back to caller. Is there any way to achieve this ?

like image 430
Kurubaran Avatar asked Mar 25 '26 19:03

Kurubaran


1 Answers

Your code activate an exe file. notice that your program, like every other program is returning a code. this return code can be achieved by doing:

ProcessStartInfo info = new ProcessStartInfo();
     info.FileName = "ConsoleApplication2.exe";
     Process p = Process.Start(info);
     p.WaitForExit();
     int eCode = p.ExitCode;

please do not do that in the main thread because it will stop until the process is stopped. in your ConsoleApplication2.exe make sure that in case of exception you'll return ( in the main function) a code(number) indicating there was an error. it is that simple:

  static int Main()
  {
     try
     {
        // My code
     }
     catch (Exception)
     {
        return 5;// Meaning error
     }

     return 0; // all went better then expected!
  }
like image 95
No Idea For Name Avatar answered Mar 28 '26 10:03

No Idea For Name