views:

56

answers:

3

Hi,

I have a pre-built .NET Console application EXE that I am launching from separate C# code using the Process class. If the application throws an unhandled exception, a window pops up (the one that says " has encountered a problem and needs to close..." and has "Debug", "Send Error Report" and "Don't Send" buttons).

If this happens, control does not return to my code until I press the "Don't send" button button, which is inconvenient for me as I want it to be able to run unattended.

Is there anyway I can suppress this pop-up somehow without changing the pre-built application, or at least end the process after a certain time has elapsed. Ideally in any solution I still want the exception to be captured in the console output so I can read it from my code - this actually does work fine as long as I manually press the "Don't Send" button, so I just want this to happen without user interaction.

Thanks...

A: 

You might be able to write a framework host and run your app in that. Any other solution I can think of would belong on Superuser or Serverfault.

Greg D
A: 

In your main routine, you would put an unhandled exception hander, that logs the error, write something to console, and/or shuts down the application (as required) and terminates.

Without modifying the app, I don't see how to do this, except perhaps run your app as a dependant type from another app that instantiates an instance of the main type in this app and then, calls Main(), from the other new app. Even though the app is an exe, the types in it can be used (and their methods can be called) from within another process as thought it was a dll library assembly. The unhandled exception will propagate to the outer wrapper app, and Then do as suggested above in the new app.

... Or can you do this in your "separate C# Code" where you are using the Process class? Do it in the main routine of that exe....

Charles Bretana
I can't catch exceptions in the external process from my calling code. I think this is expected.I did hope to at least have a timer on the launched process and Kill it if the timer pops e.g. process.WaitForExit(60000); if (!process.HasExited) process.Kill();But unfortunately Kill does not actually end the process in the case where an exception window is open. It won't end unless the user clicks a button in that window.
zeroid
A: 

The solution I ended up with was to defined a maximum time for the external process and after this time, force it to come to and end AND also kill any instances of DW20, which is the process that the error reporting window runs under.

int waitTimeSecs = 120;    

bool cleanExit = process.WaitForExit(waitTimeSecs * 1000);
if (!process.HasExited)
{
    process.CloseMainWindow();
    System.Threading.Thread.Sleep(2000);
}

if (!process.HasExited)
{
    process.Kill();
    process.WaitForExit();
    // if an exception window has popped up, that needs to be killed too
    // DW20 is the Dr. Watson application error handling process
    foreach (var process in Process.GetProcessesByName("DW20"))
    {
        process.CloseMainWindow();
        System.Threading.Thread.Sleep(2000);
        if (!process.HasExited)
            process.Kill();
    }
}

The only drawback I can see is closing other error reporting windows that might happen to open at that time, but for me this is good enough right now.

zeroid