Avoid "program stopped working" in C#/.NET
Asked Answered
B

6

35

I have a console application written in C#/.NET that I want to run from a script (nant). If an exception occurs in the console application, I would like nant to continue, but in Windows Vista there is a popup that searches for solutions and asks for debug etc.

I would like to avoid the popup with "program stopped working" when an exception happens in the console application. How can I control this from C#/.NET?

(A similar question addresses the issue for the C language, but I would like a solution for C#/.NET.)

(To clarify: I would like the exception to be passed to nant, but without the popup.)

Bronco answered 14/9, 2009 at 13:39 Comment(0)
E
46

The JIT debugger popup occurs when there's an unhandled exception. That is, an exception tunnels all the way up the stack to the root of any thread in the runtime.

To avoid this, you can handle the AppDomain.CurrentDomain.UnhandledException event and just call Environment.Exit(1) to exit gracefully.

This will handle all exceptions on all threads within your AppDomain. Unless you're doing anything special, your app probably only has one AppDomain, so putting this in your public static void Main method should suffice:

AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
    Console.Error.WriteLine("Unhandled exception: " + args.ExceptionObject);
    Environment.Exit(1);
};

You should probably use the NAnt logger to write out the error in this case too (can't recall the API for this offhand though.)

You can also disable JIT debugging on the machine. I would only recommend this in certain circumstances such as for a dedicated build server.

Ebb answered 14/9, 2009 at 13:41 Comment(3)
You should ask if you are in release-mode before, after this will kill debuggingImpersonal
Why is args.ExceptionObject not an Exception ? Is it safe to cast ?Zurek
@Zurek good question. I'd use an as cast and check for null myself.Ebb
R
2

Under Windows Vista you can disable this dialog for your programms.

Disable the "Problem Reports and Solutions feature". You find it under Control Panel-->Problem Reports and Solutions-->Change Settings-->Advanced Settings-->Turn off for my programs, problem reporting

Remontant answered 14/9, 2009 at 13:44 Comment(2)
Yes, that might do if I set it up on my own machine, but I would like it to be baked into the program such that it works consistently on other machines as well. (See accepted answer.)Bronco
can i do this on windows 10?Latisha
T
1

Just catch the exception and log/ignore it.

Thermoelectricity answered 14/9, 2009 at 13:41 Comment(2)
Yes, that avoids the popup, but it also avoids the advantage of having other programs that call this program know that some exception occurred. See the accepted answer.Bronco
You can call Environment.Exit in catch block.Thermoelectricity
F
1

The popup appears due to an unhandled exception. To avoid that make sure your main method captures all exceptions and turn them into some other useful piece of info you can pick up. Just ignoring the exception is not recommended.

Btw remember that exceptions are per thread, so if your application spawns threads or uses thread pool threads, you need a handler for these too.

Fiduciary answered 14/9, 2009 at 13:42 Comment(0)
R
0

Usually this only happens when your app doesnt handle an exception. If you wrap your whole console app in a try/catch bblock, and just pass back a fail code, then you will avoid this.

Resect answered 14/9, 2009 at 13:42 Comment(0)
T
0

Sometimes, a windows application will stop working if you are using a System.Timers.Timer.

To fix this, change System.Timers.Timer by System.Windows.Forms.Timer

Greetings

Thickhead answered 28/9, 2021 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.