Application.DispatcherUnhandledException Événement
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Se produit lorsqu’une exception est levée par une application, mais pas gérée.
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
Type d'événement
Exemples
L’exemple suivant montre comment traiter des exceptions non gérées en gérant l’événement 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
Remarques
Par défaut, Windows Presentation Foundation intercepte les exceptions non gérées, avertit les utilisateurs de l’exception d’une boîte de dialogue (à partir de laquelle ils peuvent signaler l’exception) et arrête automatiquement une application.
Toutefois, si une application doit effectuer un traitement d’exception non géré personnalisé à partir d’un emplacement centralisé, vous devez gérer DispatcherUnhandledException.
DispatcherUnhandledException est déclenché par une Application exception pour chaque exception non gérée par le code exécuté sur le thread d’interface utilisateur principal.
Si une exception n’est pas gérée sur un thread d’interface utilisateur en arrière-plan (un thread avec son propre Dispatcherthread) ou sur un thread Dispatcherde travail en arrière-plan (sans thread), l’exception n’est pas transférée au thread d’interface utilisateur principal. Par conséquent, DispatcherUnhandledException elle n’est pas levée. Dans ces circonstances, vous devez écrire du code pour effectuer les opérations suivantes :
Gérez les exceptions sur le thread d’arrière-plan.
Distribuez ces exceptions au thread d’interface utilisateur principal.
Réexédez-les sur le thread d’interface utilisateur principal sans les gérer pour qu’elles puissent DispatcherUnhandledException être déclenchées.
Pour plus d’informations, consultez la vue d’ensemble du modèle de threading .
Le DispatcherUnhandledException gestionnaire d’événements est passé à un DispatcherUnhandledExceptionEventArgs argument qui contient des informations contextuelles concernant l’exception, notamment :
Exception (Exception).
Dispatcher D’où elle provient (Dispatcher).
Vous pouvez utiliser ces informations pour déterminer si une exception est récupérable ou non. Une exception récupérable peut être un FileNotFoundException, par exemple, alors qu’une exception irrécupérable peut être un StackOverflowException, par exemple.
Lorsque vous traitez une exception non gérée à partir de DispatcherUnhandledException, et que vous ne souhaitez pas que WPF continue de le traiter, vous devez définir la Handled propriété sur true.
Contrairement aux autres événements qui Application déclenchent, DispatcherUnhandledException n’a pas d’implémentation virtuelle protégée correspondante (OnDispatcherUnhandledException). Par conséquent, les classes qui dérivent de Application doivent toujours inscrire un gestionnaire d’événements pour DispatcherUnhandledException traiter des exceptions non gérées.