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 ?

4

1 回答 1

2

您的代码激活一个 exe 文件。请注意,您的程序与其他所有程序一样正在返回代码。此返回代码可以通过执行以下操作来实现:

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

请不要在主线程中这样做,因为它会停止,直到进程停止。在您的 ConsoleApplication2.exe 中确保在异常情况下您将返回(在main函数中)一个代码(数字),表明存在错误。就是这么简单:

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

     return 0; // all went better then expected!
  }
于 2013-07-25T11:13:55.273 回答