避免"程序停止工作"在C#/。NET程序、工作、QUOT、NET

2023-09-03 00:07:51 作者:⑤彩環

我有,我想从一个剧本(南特)运行在C#编写/。NET控制台应用程序。如果在控制台应用程序时出现异常,我想恶性继续,但在Windows Vista中有一个弹出的搜索解决方案,并要求调试等。

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.

我想避免弹出的程序停止工作时,破例在控制台应用程序发生。如何控制这从C#/。NET?

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?

(类似的问题解决问题的C语言 ,但我想对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.)

推荐答案

JIT调试器弹出时,有未处理的异常发生。也就是说,一个例外隧道一路堆栈在运行时中的任何螺纹的根部

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.

要避免这种情况,你可以处理的AppDomain.CurrentDomain.UnhandledException事件并调用Environment.Exit(1)正常退出。

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

这将处理您的AppDomain中的所有线程的所有异常。除非你正在做什么特别的东西,你的应用程序可能只有一个AppDomain中,所以把这个在你的公共静态无效的主要方法就足够了:

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
    += delegate(object sender, UnhandledExceptionEventArgs args)
    {
        var exception = Exception e = (Exception) args.ExceptionObject;
        Console.WriteLine("Unhandled exception: " + exception);
        Environment.Exit(1);
    }

您也许应该使用楠记录写在这种情况下,误差太大(不记得这个副手的API,但。)

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.)

您也可以禁用JIT调试在机器上。我只会在某些情况下,例如有专门的构建服务器推荐这个。

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