views:

150

answers:

2

I have a C# app which invokes an unmanaged C++ application via Process.Start()

On some machines, if this C++ app aborts, I'm left with the Just-In-Time error dialog showing

Is there a way from C# to detect that the C++ app has errored and just restart it

(I don't have the source to and therefore can't modify the C++ app)

A: 

If the C++ is a console based you can check two things,

  1. The exitcode, commonly 0 means closed normally otherwise is problem (but I Think this depends on the convention used to develop this applications)

  2. Check the standard Error if it is empty, so everything is ok

    using(Process p = new Process()) {

     p.StartInfo.UseShellExecute = false;
     p.StartInfo.RedirectStandardError = true;
     p.StartInfo.FileName = "any.exe";
     p.Start();
     string error = p.StandardError.ReadToEnd();
     p.WaitForExit()
     if(error.Length == 0 && p.ExitCode == 0)
     {
     }
    }
    
Ahmed Said
+1  A: 

The JIT debugger dialog is showing because the C++ app crashed in some way. Whilst the dialog is open, the process is still present (but suspended) which means that the Process class wont consider it to have "exited" until the JIT dialogue is dismissed or an attached debugger ends the process.

You could either disable JIT debugging on the machines experiencing the problem (I think that disabling the Machine Debug Manager service may disable JIT debugging), allowing the app to crash and die (and, therefore, report as "exited"), or (and I've no idea how you would go about doing this) see if you can make your host C# app attach itself as a debugger for the C++ app and receive notification of the crash from the C++ app, terminate it, and then act upon the information it has ended.

Jason Musgrove