2010-10-22 2 views
0

Juste une petite question.Est-il impossible d'afficher des formulaires lors de la gestion d'une application AppDomain.CurrentDomain.UnhandledException?

Je peux obtenir un formulaire pour montrer à d'autres exceptions, mais avec le type que je demande au sujet, je reçois le système « application ne fonctionne plus » dialogue:

Program.cs:

 #if !DEBUG 
     // Add the event handler for handling UI thread exceptions to the event. 
     Application.ThreadException += new ThreadExceptionEventHandler(Logging.Application_ThreadException); 

     // 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(Logging.CurrentDomain_UnhandledException); 
     #endif 

     throw new Exception(); 

Logging.cs:

public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
    { 
     ExceptionHandler((Exception)e.ExceptionObject, true); 
    } 

    private static void ExceptionHandler(Exception e, bool isFatal) 
    { 
     LogException(e, isFatal); 

     //if (!isFatal) 
     //{ 
      FormException formException = new FormException(isFatal); 
      formException.Show(); 
     //} 
     //else // It seems that showing a form when you have an unhandled exception isn't a good idea... 
     //{ 
     // MessageBox.Show("Crashed", 
     //  Program.Name, 
     //  MessageBoxButtons.OK, 
     //  MessageBoxIcon.Stop); 
     // Program.Exit(); 
     //} 
    } 

Répondre

2

Peut-être que cet article sera utile A Simple Class to Catch Unhandled Exceptions in WinForms

EDIT: Je me vérifier:

static void Main() 
    { 
     AppDomain.CurrentDomain.UnhandledException += (sender, args) => 
                  { 
                   using (var form = new Form1()) 
                   { 
                    form.ShowDialog(); 
                   } 
                  }; 

     throw new Exception(); 
    } 

fonctionné comme prévu. Alors pouvez-vous montrer votre code qui ne fonctionne pas?


également de l'article:

public UnhandledExceptionDlg() 
    { 
     // Add the event handler for handling UI thread exceptions to the event: 
     Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction); 

     // 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(UnhandledExceptionFunction); 
    } 


    private void UnhandledExceptionFunction(Object sender, UnhandledExceptionEventArgs args) 
    { 
     // Suppress the Dialog in Debug mode: 
     #if !DEBUG 
     ShowUnhandledExceptionDlg((Exception)args.ExceptionObject); 
     #endif 
    } 

    private void ShowUnhandledExceptionDlg(Exception e) 
    { 
     Exception unhandledException = e; 

     if(unhandledException == null) 
      unhandledException = new Exception("Unknown unhandled Exception was occurred!"); 

     UnhandledExDlgForm exDlgForm = new UnhandledExDlgForm(); 
     try 
     { 
      string appName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; 
      exDlgForm.Text = appName; 
      exDlgForm.labelTitle.Text = String.Format(exDlgForm.labelTitle.Text, appName); 
      exDlgForm.checkBoxRestart.Text = String.Format(exDlgForm.checkBoxRestart.Text, appName); 
      exDlgForm.checkBoxRestart.Checked = this.RestartApp; 

      // Do not show link label if OnShowErrorReport is not handled 
      exDlgForm.labelLinkTitle.Visible = (OnShowErrorReport != null); 
      exDlgForm.linkLabelData.Visible = (OnShowErrorReport != null); 

      // Disable the Button if OnSendExceptionClick event is not handled 
      exDlgForm.buttonSend.Enabled = (OnSendExceptionClick != null); 

      // Attach reflection to checkbox checked status 
      exDlgForm.checkBoxRestart.CheckedChanged += delegate(object o, EventArgs ev) 
      { 
       this._dorestart = exDlgForm.checkBoxRestart.Checked; 
      }; 

      // Handle clicks on report link label 
      exDlgForm.linkLabelData.LinkClicked += delegate(object o, LinkLabelLinkClickedEventArgs ev) 
      { 
       if(OnShowErrorReport != null) 
       { 
        SendExceptionClickEventArgs ar = new SendExceptionClickEventArgs(true, unhandledException, _dorestart); 
        OnShowErrorReport(this, ar); 
       } 
      }; 

      // Show the Dialog box: 
      bool sendDetails = (exDlgForm.ShowDialog() == System.Windows.Forms.DialogResult.Yes); 

      if(OnSendExceptionClick != null) 
      { 
       SendExceptionClickEventArgs ar = new SendExceptionClickEventArgs(sendDetails, unhandledException, _dorestart); 
       OnSendExceptionClick(this, ar); 
      } 
     } 
     finally 
     { 
      exDlgForm.Dispose(); 
     } 
    } 

il montre donc la forme aussi ...

+0

L'exemple fourni utilise un MessageBox. – unrelativity

+0

Vérifiez sa source ... –

+0

Je suis corrigé. Mais l'exécution de la solution a des exceptions brutales ou non gérées qui sont interceptées par VC# – unrelativity

Questions connexes