MessageBox的时候“未处理的异常”中的WinForms
触发,所以我尝试添加“AppDomain.CurrentDomain.UnhandledException
”处理程序,以我的应用程序和它的工作确定,如果我错误记录到一个文本文件中。但是当我尝试使用MessageBox时,它永远不会弹出。它是.Net中的另一个bug吗?有任何想法吗?MessageBox的时候“未处理的异常”中的WinForms
这里是我的代码示例:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
,这里是我的处理方法:
static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show("Whoops! Please contact the developers with "
+ "the following information:\r\n\r\n" + ex.Message + ex.StackTrace,
"Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
编辑:我已经尝试了所有可能的选择,但我还是不能看消息框。现在的问题是,当我从Visual C#运行它(调试模式)时,它完美地显示了该框。但是当我直接从调试运行应用程序/文件夹释放它不会显示在MessageBox和应用程序继续运行,就像没有错误发生...
这个例子在调试模式对我的作品,并释放与VS2010模式或不:
using System;
using System.Windows.Forms;
namespace WinForms*SpielWiese
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main() {
System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
try {
var exception = e.Exception != null ? e.Exception.Message + e.Exception.StackTrace : string.Empty;
MessageBox.Show("Whoops! Please contact the developers with the following information:\r\n\r\n" + exception,
"Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally {
Application.Exit();
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
try {
var exception = e.ExceptionObject is Exception ? ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).StackTrace : string.Empty;
MessageBox.Show("Whoops! Please contact the developers with the following information:\r\n\r\n" + exception,
"Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally {
Application.Exit();
}
}
}
}
代码形式
编辑现在有一个定时器,具有相同的结果....
using System.Windows.Forms;
namespace WinForms*SpielWiese
{
public partial class Form1 : Form
{
private System.Threading.Timer myTimer;
public Form1() {
this.InitializeComponent();
this.myTimer = new System.Threading.Timer(state =>
{
var i = 0;
var s = 100/i;
}
, null, 5000, 5000);
}
}
}
这是部分工作。但是当异常发生在不同的位置时,它永远不会弹出...我使用了一个计时器,并且每3秒从该计时器运行一个函数。该函数实际上是从一个新的线程执行的。现在我真的不知道这是什么原因,为什么会发生这种情况。但我认为它是一个比看起来更大的问题......无论如何 – SolidSnake 2012-02-08 13:14:03
的情况下,可能是因为Unhandled
Exceptions
导致应用程序静默终止并且UnhandledExceptionEventHandler
处理非UI线程异常。
见Application.SetUnhandledExceptionMode方法,AppDomain.UnhandledException事件和Application.ThreadException事件
编辑: 尝试设置Application.SetUnhandledExceptionMode
按第一环节:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //add this line
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
不是这样的情况。我已经尝试了所有可能的选项,但仍然看不到MessageBox。现在的问题是,当我从Visual C#运行它(调试模式)时,它完美地显示了该框。但是当我直接从debug/release文件夹运行应用程序时,它从不显示MessageBox ... – SolidSnake 2012-02-08 12:18:55
@RobinVanPersi在订阅事件之前尝试使用Application.SetUnhandledExceptionMode方法..请参阅编辑。 – VS1 2012-02-08 12:43:36
同样的事情...没有任何改变..请尝试直接从调试文件夹运行生成的应用程序.. – SolidSnake 2012-02-08 12:58:11
我没有问题与您的代码,一切工作正常....(我有tryed与.net 2.0和4.0编译,两个作品) – punker76 2012-02-08 12:03:11
@ punker76 - 嗯这很奇怪。你在64位机器上? – SolidSnake 2012-02-08 12:15:18
我编译为在64位机Win7和VS2010 – punker76 2012-02-08 12:27:21