在 C#/.NET 中避免“程序停止工作”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1421597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:21:27  来源:igfitidea点击:

Avoid "program stopped working" in C#/.NET

c#.netexceptionwindows-vistapopup

提问by Ole Lynge

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 编写的控制台应用程序,我想从脚本 (nant) 运行它。如果控制台应用程序中发生异常,我希望继续,但在 Windows Vista 中,有一个弹出窗口搜索解决方案并要求调试等。

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#/.NET 控制它?

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

(一个类似的问题解决了 C 语言的问题,但我想要一个 C#/.NET 的解决方案。)

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

(澄清:我希望将异常传递给 nant,但没有弹出窗口。)

采纳答案by Drew Noakes

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.

当存在未处理的异常时,会出现 JIT 调试器弹出窗口。也就是说,异常在堆栈中一直向上传输到运行时中任何线程的根。

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

为避免这种情况,您可以处理AppDomain.CurrentDomain.UnhandledException事件并Environment.Exit(1)优雅地调用退出。

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 Mainmethod should suffice:

这将处理 AppDomain 中所有线程上的所有异常。除非你做任何特别的事情,否则你的应用程序可能只有一个 AppDomain,所以把它放在你的public static void Main方法中就足够了:

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

在这种情况下,您可能也应该使用 NAnt 记录器来写出错误(尽管无法立即回忆起这个 API。)

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

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

回答by maciejkow

Just catch the exception and log/ignore it.

只需捕获异常并记录/忽略它。

回答by Neil N

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.

通常这只会在您的应用程序不处理异常时发生。如果您将整个控制台应用程序包装在一个 try/catch bblock 中,并且只传回一个失败代码,那么您将避免这种情况。

回答by Brian Rasmussen

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.

顺便说一下,异常是每个线程的,所以如果你的应用程序产生线程或使用线程池线程,你也需要一个处理程序。

回答by Jehof

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

在 Windows Vista 下,您可以为您的程序禁用此对话框。

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

禁用“问题报告和解决方案功能”。您可以在控制面板-->问题报告和解决方案-->更改设置-->高级设置-->关闭我的程序,问题报告下找到它