Condividi tramite


Application.DispatcherUnhandledException Evento

Definizione

Si verifica quando un'eccezione viene generata da un'applicazione ma non 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 le 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 intercetta le 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 l'elaborazione personalizzata di eccezioni non gestite da una posizione centralizzata, è necessario gestire DispatcherUnhandledException.

DispatcherUnhandledException viene generato da un oggetto Application 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 ), Dispatcherl'eccezione non viene inoltrata al thread principale dell'interfaccia utente. Di conseguenza, DispatcherUnhandledException non viene generato. In queste circostanze, sarà necessario scrivere codice per eseguire le operazioni seguenti:

  1. Gestire le eccezioni nel thread in background.

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

  3. Rigenerarli nel thread principale dell'interfaccia utente senza gestirli per consentire DispatcherUnhandledException la generazione.

Per altre informazioni, vedere Panoramica del modello di threading .

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

È possibile utilizzare queste informazioni per determinare se un'eccezione è recuperabile o meno. Un'eccezione ripristinabile potrebbe essere , FileNotFoundExceptionad esempio, mentre un'eccezione irreversibile potrebbe essere , StackOverflowExceptionad 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 dispone di un'implementazione virtuale protetta corrispondente (OnDispatcherUnhandledException). Di conseguenza, le classi che derivano da Application devono sempre registrare un gestore eventi con DispatcherUnhandledException per elaborare le eccezioni non gestite.

Si applica a