Application.DispatcherUnhandledException Evento

Definizione

Si verifica quando un'eccezione viene generata da un'applicazione ma non viene gestita.

public:
 event System::Windows::Threading::DispatcherUnhandledExceptionEventHandler ^ DispatcherUnhandledException;
public event System.Windows.Threading.DispatcherUnhandledExceptionEventHandler DispatcherUnhandledException;
member this.DispatcherUnhandledException : System.Windows.Threading.DispatcherUnhandledExceptionEventHandler 
Public Custom Event DispatcherUnhandledException As DispatcherUnhandledExceptionEventHandler 

Tipo evento

Esempio

Nell'esempio seguente viene illustrato come elaborare eccezioni non gestite gestendo l'evento DispatcherUnhandledException .

using System.Windows;
using System.Windows.Threading;

namespace SDKSample
{
    public partial class App : Application
    {
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // Process unhandled exception

            // Prevent default unhandled exception processing
            e.Handled = true;
        }
    }
}
Imports System.Windows
Imports System.Windows.Threading

Namespace SDKSample
    Partial Public Class App
        Inherits Application
        Private Sub App_DispatcherUnhandledException(ByVal sender As Object, ByVal e As DispatcherUnhandledExceptionEventArgs)
            ' Process unhandled exception

            ' Prevent default unhandled exception processing
            e.Handled = True
        End Sub
    End Class
End Namespace

Commenti

Per impostazione predefinita, Windows Presentation Foundation rileva eccezioni non gestite, notifica agli utenti dell'eccezione da una finestra di dialogo (da cui possono segnalare l'eccezione) e arresta automaticamente un'applicazione.

Tuttavia, se un'applicazione deve eseguire un'elaborazione di eccezioni non gestita personalizzata da una posizione centralizzata, è consigliabile gestire DispatcherUnhandledException.

DispatcherUnhandledException viene generato da un Application oggetto per ogni eccezione non gestita dal codice in esecuzione nel thread principale dell'interfaccia utente.

Se un'eccezione non viene gestita in un thread dell'interfaccia utente in background (un thread con il proprio Dispatcher) o un thread di lavoro in background (un thread senza Dispatcherun ), l'eccezione non viene inoltrata al thread dell'interfaccia utente principale. Di conseguenza, DispatcherUnhandledException non viene generato. In queste circostanze, è necessario scrivere codice per eseguire le operazioni seguenti:

  1. Gestire le eccezioni nel thread in background.

  2. Inviare tali eccezioni al thread dell'interfaccia utente principale.

  3. Rethrowli nel thread dell'interfaccia utente principale senza gestirli per consentire DispatcherUnhandledException di generare.

Per altre informazioni, vedere Panoramica del modello di threading .

Il DispatcherUnhandledException gestore eventi viene passato un DispatcherUnhandledExceptionEventArgs argomento contenente informazioni contestuali relative all'eccezione, tra cui:

È possibile usare queste informazioni per determinare se un'eccezione è recuperabile o meno. Un'eccezione ripristinabile potrebbe essere , ad esempio, mentre un'eccezione non recuperabile potrebbe essere , FileNotFoundExceptionStackOverflowExceptionad esempio.

Quando si elabora un'eccezione non gestita da DispatcherUnhandledExceptione non si vuole che WPF continui l'elaborazione, è necessario impostare la Handled proprietà su true.

A differenza degli altri eventi generati Application , DispatcherUnhandledException non è disponibile un'implementazione virtuale protetta corrispondente (OnDispatcherUnhandledException). Di conseguenza, le classi derivate da Application devono sempre registrare un gestore eventi con DispatcherUnhandledException per elaborare eccezioni non gestite.

Si applica a