2009-11-05 6 views
7

Esiste un modo (diverso da WerAddExcludedApplication che non funziona in Windows XP) per disabilitare la finestra "L'applicazione ha riscontrato un problema e deve chiudersi" dall'apparire quando la mia applicazione si arresta in modo anomalo?Disabilita la finestra "Foo ha riscontrato un problema e deve chiudere"

alt text http://i37.tinypic.com/2vvw6yd.png

(immagine presa da Bil Simser' blog)

ho bisogno di questo per lavorare in Windows XP.

+10

Sì, mantenere l'app da arresto anomalo. –

risposta

-4

Non hanno eccezioni non gestite.

+1

non è possibile gestire tutte le eccezioni ... almeno non è sempre possibile –

+1

Sì, è possibile. Gestisci tutte le eccezioni a cui puoi pensare, quindi aggiungi un gestore per le eccezioni non gestite per catturare quelle che non hai. Questo funziona circa il 99,9% delle volte; per l'altro 0,01%, correggi il tuo codice errato. –

+2

Non è una risposta molto utile! –

1

È necessario gestire gli eventi Application.ThreadException e AppDomain.CurrentDomain.UnhandledException.

+0

Questi sono eventi in modo che tu possa agire su di loro, non molto da gestire, ma questo è oltre il punto. Potresti non avere altre opzioni per chiudere l'app una volta danneggiata e cosa fare allora? –

+0

Inoltre non funzionerà comunque - anche se ti iscrivi all'evento, la finestra apparirà comunque –

+0

Oh, dovresti chiudere definitivamente l'applicazione se arrivi a questo stadio, ma non vedo un grosso problema con la presentazione una finestra di arresto anomalo personalizzata. Per quanto riguarda il tuo secondo commento, tutto quello che posso dire è che funziona qui. – Simon

2

Il mio suggerimento è, invece di disabilitare questa finestra di dialogo, registrarsi con Microsoft in modo da poter vedere i rapporti di errore che catturiamo! In questo modo, puoi usare questa finestra di dialogo, invece di provare a sopprimerla.

http://msdn.microsoft.com/en-us/isv/bb190483.aspx

+0

grazie, ma in questo caso * davvero * non è una soluzione. Ho bisogno in particolare di ciò che chiedo - possibilità di riavviare l'app quando si blocca senza che la finestra di dialogo spuntasse. –

7

Questo lavoro, che permette di mostrare la propria finestra di dialogo personalizzata:

Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction); 
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction); 

Ecco un esempio completo da MSDN:

Thread newThread = null; 

// Starts the application. 
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)] 
public static void Main(string[] args) 
{ 
    // Add the event handler for handling UI thread exceptions to the event. 
    Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_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(CurrentDomain_UnhandledException); 

    // Runs the application. 
    Application.Run(new ErrorHandlerForm()); 
} 

// Programs the button to throw an exception when clicked. 
private void button1_Click(object sender, System.EventArgs e) 
{ 
    throw new ArgumentException("The parameter was invalid"); 
} 

// Start a new thread, separate from Windows Forms, that will throw an exception. 
private void button2_Click(object sender, System.EventArgs e) 
{ 
    ThreadStart newThreadStart = new ThreadStart(newThread_Execute); 
    newThread = new Thread(newThreadStart); 
    newThread.Start(); 
} 

// The thread we start up to demonstrate non-UI exception handling. 
void newThread_Execute() 
{ 
    throw new Exception("The method or operation is not implemented."); 
} 

// Handle the UI exceptions by showing a dialog box, and asking the user whether 
// or not they wish to abort execution. 
private static void Form1_UIThreadException(object sender, ThreadExceptionEventArgs t) 
{ 
    DialogResult result = DialogResult.Cancel; 
    try 
    { 
     result = ShowThreadExceptionDialog("Windows Forms Error", t.Exception); 
    } 
    catch 
    { 
     try 
     { 
      MessageBox.Show("Fatal Windows Forms Error", 
       "Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop); 
     } 
     finally 
     { 
      Application.Exit(); 
     } 
    } 

    // Exits the program when the user clicks Abort. 
    if (result == DialogResult.Abort) 
     Application.Exit(); 
} 

// Handle the UI exceptions by showing a dialog box, and asking the user whether 
// or not they wish to abort execution. 
// NOTE: This exception cannot be kept from terminating the application - it can only 
// log the event, and inform the user about it. 
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
    try 
    { 
     Exception ex = (Exception)e.ExceptionObject; 
     string errorMsg = "An application error occurred. Please contact the adminstrator " + 
      "with the following information:\n\n"; 

     // Since we can't prevent the app from terminating, log this to the event log. 
     if (!EventLog.SourceExists("ThreadException")) 
     { 
      EventLog.CreateEventSource("ThreadException", "Application"); 
     } 

     // Create an EventLog instance and assign its source. 
     EventLog myLog = new EventLog(); 
     myLog.Source = "ThreadException"; 
     myLog.WriteEntry(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace); 
    } 
    catch (Exception exc) 
    { 
     try 
     { 
      MessageBox.Show("Fatal Non-UI Error", 
       "Fatal Non-UI Error. Could not write the error to the event log. Reason: " 
       + exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop); 
     } 
     finally 
     { 
      Application.Exit(); 
     } 
    } 
} 

// Creates the error message and displays it. 
private static DialogResult ShowThreadExceptionDialog(string title, Exception e) 
{ 
    string errorMsg = "An application error occurred. Please contact the adminstrator " + 
     "with the following information:\n\n"; 
    errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace; 
    return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore, 
     MessageBoxIcon.Stop); 
} 
+0

E se non sono in un'applicazione WinForms? ma diciamo ... App Console, dove non uso la classe Application? –

1

Non è una soluzione programmatica, ma puoi farlo modificando le impostazioni di segnalazione degli errori di Windows.

  1. controllo Aprire il Pannello di
  2. Open System, finestra di dialogo Proprietà del sistema si aprirà
  3. Vai alla scheda Avanzate
  4. Cliccare sulla segnalazione degli errori, errori dialogo Segnalazione aprirà
  5. Se si desidera disabilitare su una singola applicazione, fare clic sul pulsante Scegli programmi, verrà visualizzata una finestra di dialogo
  6. Fare clic sul (secondo) pulsante Aggiungi per aggiungere l'applicazione all'elenco "Non segnalare errori per questi programmi".
  7. Confermare tutte e tre le finestre di dialogo ...
Problemi correlati