14

I'm launching an external process with System.Diagnostics.Process. This is part of a batch job, so if one process crashes, I'd like to handle it and let the rest continue.

What currently happens is Windows pops up a dialog telling me that the program has crashed, and only once I've dismissed that manually does the process exit.

According to this question, the Process.Responding property is only available for programs with UIs (the process I'm launching is a console app).

I also looked at the various events a process provides, but none of them are fired on crashing.

Any ideas?

Community
  • 1
  • 1
meatvest
  • 879
  • 6
  • 11

2 Answers2

25

Try setting the registry following registry value to value DWORD 2:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\ErrorMode = 2

This will affect every process on the machine.

Reference: How to Get Rid of System and Application Popup Messages

If you have the source code to the program that crashes, you can prevent the popups by catching all structured exceptions and exiting without popping up a message box. How you do this depends on the programming language used.

If you don't have the source, use the SetErrorMode function in the parent to suppress popups. The error mode is inherited by subprocesses. You must set UseShellExecute to false for this to work:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;


namespace SubProcessPopupError
{

    class Program
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern int SetErrorMode(int wMode);

        static void Main(string[] args)
        {
            int oldMode = SetErrorMode(3);
            Process p;
            ProcessStartInfo ps = new ProcessStartInfo("crash.exe");
            ps.UseShellExecute = false;
            p = Process.Start(ps);
            SetErrorMode(oldMode);
            p.WaitForExit();
        }
    }
}

If you are getting a dialog saying "Do you want to debug using the selected debugger?", you can turn that off by setting this registry value to 0.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug\Auto = 0

However, I don't think this will come up if you have set the error mode to 3 as explained above.

Jacob
  • 77,566
  • 24
  • 149
  • 228
Carlos A. Ibarra
  • 6,002
  • 1
  • 28
  • 38
  • Looks like this would work, but it's a bit like using a sword to spread butter. This program will have to run on other people's computers so altering system-wide properties isn't an option :( – meatvest Mar 23 '09 at 14:04
  • See the extra paragraph I added to my answer. If you have the source code...catch all exceptions. – Carlos A. Ibarra Mar 23 '09 at 14:11
  • The crash that prompted my question was actually caused during the loading of the program's references - the Main() method hadn't even been entered - meaning the exception was (as far as I understand) impossible to catch. – meatvest Mar 23 '09 at 14:14
  • OK, look at the example I just added. It uses SetErrorMode(). – Carlos A. Ibarra Mar 23 '09 at 14:45
  • 1
    I tried your code and the message is still appearing (I even tried setting the error mode in the crashing process in a toy app). Talking to a colleague revealed that I may be getting a different type of dialog - the one that includes the "Debug" option - because I have the Windows SDK installed. – meatvest Mar 23 '09 at 16:44
  • I added another sentence on how to turn off the debugger dialog, but my testing shows it doesn't pop up if you SetErrorMode(3). If that is what you are seeing, I can't explain why it pops up for you. – Carlos A. Ibarra Mar 23 '09 at 18:01
  • This answer covered all the bases I needed for this same issue. Thanks for being so thorough! – Jeremy Murray Mar 28 '11 at 16:43
  • 2
    SetErrorMode(3) does the trick for me. It only affects the current process and child processes, which suits my application. – Boinst Jun 27 '12 at 00:01
  • Even though it only affects the process / child process, I still restore the original value in the finally block *after* the `WaitForExit` call. – BrainSlugs83 Sep 07 '14 at 21:15
  • Additionally -- I have the Windows SDK installed, and I do not see this message -- @CarlosA.Ibarra are you sure your process doesn't contain a `Debugger.Launch` command? -- that will open up the dialog you mentioned. – BrainSlugs83 Sep 07 '14 at 21:16
0

Another option is to run your executable under cdb, and then search for the word "exception", using the following command line:

cdb.exe -G -g -c "Q" <your executable>
Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
Devin
  • 87
  • 6