如何使用ThreadException? [英] How to use ThreadException?
问题描述
我试图用
<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx#Y399" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx#Y399
但是当我这样做
抛出新ArgumentNullException(播放列表为空);
我什么也没得到。我敢打赌,我失去了一些东西很明显的。
这是我的code。
使用系统;
使用System.Security.Permissions;
使用System.Windows.Forms的;
使用的System.Threading;
命名空间MediaPlayer.NET
{
内部静态类节目
{
///&LT;总结&gt;
///的主入口点的应用程序。
///&LT; /总结&gt;
[STAThread]
[的SecurityPermission(SecurityAction.Demand,旗帜= SecurityPermissionFlag.ControlAppDomain)
私有静态无效的主要()
{
//添加事件处理程序处理UI线程例外事件。
Application.ThreadException + =新ThreadExceptionEventHandler(UIThreadException);
//设置未处理的异常模式迫使所有的Windows窗体的错误经历
//我们的处理程序。
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//添加事件处理程序处理非UI线程例外事件。
AppDomain.CurrentDomain.UnhandledException + =
新UnhandledExceptionEventHandler(UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.Run(新MediaPlayerForm());
}
私有静态无效UnhandledException(对象发件人,UnhandledExceptionEventArgs E)
{
的MessageBox.show(UnhandledException !!!!);
}
私有静态无效UIThreadException(对象发件人,ThreadExceptionEventArgs T)
{
的MessageBox.show(UIThreadException !!!!
UIThreadException !!!!,MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Stop);
Application.Exit();
}
}
}
您code正常工作,不会有很多可能的故障模式我能想到的。除了一个人,当您调试32位code有问题在调试器和Windows SEH之间的相互作用之前的Windows 8,这是一个64位操作系统可能会导致异常被没有任何诊断,当他们出现在窗体的Load事件或的OnLoad()方法重写吞噬。检查链接后寻求解决方法,最简单的一种是项目+属性,生成标签,平台目标=值为anycpu,取消选中preFER 32位,如果你看到它。
在一般情况下,你是不会让一个Application.ThreadException的默认异常处理显示的对话框中做适当的事情。但保持它的简单,像这样做:
#ifndef的DEBUG
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
#ENDIF
现在,你再也不用担心ThreadException了,所有的异常触发AppDomain.UnhandledException事件处理程序。和周围的code中的的#ifdef您仍然可以调试未处理的异常,当引发异常调试器会自动停止。
这对UnhandledException方法添加到prevent的Windows故障通知,显示出来:
Environment.Exit(1);
I tried using
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx#Y399
but when I do this
throw new ArgumentNullException("playlist is empty");
I get nothing. I bet I'm missing something very obvious.
here is my code.
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();
}
}
}
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.
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:
#ifndef DEBUG
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
#endif
Now you never have to worry about a ThreadException anymore, all exceptions trigger the AppDomain.UnhandledException event handler. And the #ifdef around the code still lets you debug an unhandled exception, the debugger will automatically stop when the exception is raised.
Add this to the UnhandledException method to prevent the Windows crash notification from showing up:
Environment.Exit(1);
这篇关于如何使用ThreadException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!