3

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 ?

Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • 2
    Have you seen [this](http://stackoverflow.com/questions/2709198/process-start-get-errors-from-command-prompt-window?rq=1)? – Sam Jul 25 '13 at 10:59
  • Thanks Sam, Will give a try now. – Kurubaran Jul 25 '13 at 11:00
  • @Sam no luck with that, succeeding code within button click is executed. also exception thrown from exe is not caught within win form. – Kurubaran Jul 25 '13 at 11:08

1 Answers1

2

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!
  }
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • Exceptions in any sub thread are not caught with this try/catch block. Check out http://stackoverflow.com/a/878270/1134155 – Hemario Jul 25 '13 at 11:20
  • dear @Hemario this is just a sample. i can't tell what will he do in his code in ConsoleApplication2.exe, just him a general idea of what he need to do – No Idea For Name Jul 25 '13 at 11:33
  • @NoIdeaForName: just adding additional information for the OP – Hemario Jul 25 '13 at 11:39