2012-02-08 26 views
1

觸發,所以我嘗試添加「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和應用程序繼續運行,就像沒有錯誤發生...

+0

我沒有問題與您的代碼,一切工作正常....(我有tryed與.net 2.0和4.0編譯,兩個作品) – punker76 2012-02-08 12:03:11

+0

@ punker76 - 嗯這很奇怪。你在64位機器上? – SolidSnake 2012-02-08 12:15:18

+0

我編譯爲在64位機Win7和VS2010 – punker76 2012-02-08 12:27:21

回答

1

這個例子在調試模式對我的作品,並釋放與VS2010模式或不:

using System; 
using System.Windows.Forms; 

namespace WinFormsStackOverflowSpielWiese 
{ 
    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 WinFormsStackOverflowSpielWiese 
{ 
    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); 
    } 
    } 
} 
+0

這是部分工作。但是當異常發生在不同的位置時,它永遠不會彈出...我使用了一個計時器,並且每3秒從該計時器運行一個函數。該函數實際上是從一個新的線程執行的。現在我真的不知道這是什麼原因,爲什麼會發生這種情況。但我認爲它是一個比看起來更大的問題......無論如何 – SolidSnake 2012-02-08 13:14:03

1

的情況下,可能是因爲UnhandledExceptions導致應用程序靜默終止並且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()); 
+0

不是這樣的情況。我已經嘗試了所有可能的選項,但仍然看不到MessageBox。現在的問題是,當我從Visual C#運行它(調試模式)時,它完美地顯示了該框。但是當我直接從debug/release文件夾運行應用程序時,它從不顯示MessageBox ... – SolidSnake 2012-02-08 12:18:55

+0

@RobinVanPersi在訂閱事件之前嘗試使用Application.SetUnhandledExceptionMode方法..請參閱編輯。 – VS1 2012-02-08 12:43:36

+0

同樣的事情...沒有任何改變..請嘗試直接從調試文件夾運行生成的應用程序.. – SolidSnake 2012-02-08 12:58:11