更新时间:2023-11-20 18:26:28
public static void Main(string[] args)
{
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new
ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);
// Set the unhandled exception mode to force all Windows Forms errors
// to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
您可能还希望在调试时不捕获异常,因为这样更易于调试.这有点像黑客,但为此你可以用
You might also want to not catch exceptions when debugging, as this makes it easier to debug. It is somewhat of a hack, but for that you can wrap the above code around with
if (!AppDomain.CurrentDomain.FriendlyName.EndsWith("vshost.exe")) { ... }
为了防止在调试时捕获异常.
To prevent catching the exceptions when debugging.
编辑:另一种检查在调试器中运行的应用程序的方法,比检查文件名更简洁.
EDIT: An alternate way to check for your application running inside a debugger that feels cleaner than checking a filename.
(见meltform、Kiquenet 和Doug 的评论)
(see comments by moltenform, Kiquenet and Doug)
if(!System.Diagnostics.Debugger.IsAttached) { ... }
这避免了使用与 vshost.exe
不同的调试器的问题.
This avoids the problem of using a different debugger than vshost.exe
.