如何使用ThreadException? [英] How to use ThreadException?

查看:131
本文介绍了如何使用ThreadException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx#Y399

但是当我这样做

throw new ArgumentNullException("playlist is empty");

我什么都没收。我打赌我错过了一些非常明显的东西。

I get nothing. I bet I'm missing something very obvious.

这里是我的代码。

using System;
using System.Security.Permissions;
using System.Windows.Forms;
using System.Threading;

namespace MediaPlayer.NET
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
        private static void Main()
        {
            // Add the event handler for handling UI thread exceptions to the event.
            Application.ThreadException += new ThreadExceptionEventHandler(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(UnhandledException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MediaPlayerForm());
        }

        private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show("UnhandledException!!!!");
        }

        private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
        {
            MessageBox.Show("UIThreadException!!!!",
                            "UIThreadException!!!!", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
            Application.Exit();
        }
    }
}


推荐答案

你的代码工作正常,没有很多可能的故障模式我可以想到。除了一个,在调试器和Windows SEH之间的交互中有一个问题,当您在64位调试器上调试32位代码时, Windows 8之前的这个操作系统。这可能会导致异常吞吐,而不会在窗体的Load事件或OnLoad()方法覆盖中发生任何诊断。检查链接的帖子的解决方法,最简单的是项目+属性,构建选项卡,平台目标= AnyCPU,如果您看到,请选择喜欢32位。

Your code works fine, there are not a lot of possible failure modes I can think of. Except one, there's a problem in the interaction between the debugger and Windows SEH when you debug 32-bit code on a 64-bit operating system prior to Windows 8. This can cause exceptions to be swallowed without any diagnostic when they occur in the form's Load event or OnLoad() method override. Check the linked post for workarounds, the simplest one is Project + Properties, Build tab, Platform Target = AnyCPU, untick "Prefer 32-bit" if you see it.

一般来说,您正在通过不让Application.ThreadException的默认异常处理显示对话框来做适当的事情。但是保持简单,这样做:

In general, you are doing the appropriate thing by not letting the default exception handling of an Application.ThreadException display the dialog. But keep it simple, do it like this:

#if (!DEBUG)
      Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
#endif

现在,您永远不用担心ThreadException,所有异常都会触发AppDomain.UnhandledException事件处理程序。而代码中的#if仍然允许您调试未处理的异常,调试器将在引发异常时自动停止。

Now you never have to worry about a ThreadException anymore, all exceptions trigger the AppDomain.UnhandledException event handler. And the #if around the code still lets you debug an unhandled exception, the debugger will automatically stop when the exception is raised.

将此添加到UnhandledException方法以防止Windows崩溃通知显示:

Add this to the UnhandledException method to prevent the Windows crash notification from showing up:

        Environment.Exit(1);

这篇关于如何使用ThreadException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆