andrew4582

App UnhandledException WPF

Aug 11th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Windows;
  4.  
  5. namespace YourNameSpace{
  6.  
  7.     /// <summary>
  8.     /// Interaction logic for App.xaml
  9.     /// </summary>
  10.     public partial class App:Application {
  11.         protected override void OnStartup(StartupEventArgs e)
  12.         {
  13.             this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
  14.             AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  15.             base.OnStartup(e);
  16.         }
  17.  
  18.         void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  19.         {
  20.             if (e.ExceptionObject != null && !e.IsTerminating)
  21.             {
  22.                 string msg = e.ExceptionObject.ToString();
  23.                 MessageBox.Show(msg, "AppDomain Unhandled Exception", MessageBoxButton.OK, MessageBoxImage.Information);
  24.             }
  25.  
  26.             try
  27.             {
  28.                 Process.GetCurrentProcess().Kill();
  29.                 AppDomain.Unload(AppDomain.CurrentDomain);
  30.             }
  31.             catch { }
  32.            
  33.         }
  34.  
  35.         void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  36.         {
  37.             try
  38.             {
  39.                 if (e.Exception != null)
  40.                 {
  41.                     string msg = "";
  42.                    
  43.                     if (e.Exception.InnerException != null)
  44.                     {
  45.                         msg = e.Exception.InnerException.Message;
  46.                         msg += Environment.NewLine;
  47.                         msg += Environment.NewLine;
  48.                         msg += e.Exception.InnerException.ToString();
  49.                     }
  50.                     else
  51.                     {
  52.                         msg = e.Exception.Message;
  53.                         msg += Environment.NewLine;
  54.                         msg += Environment.NewLine;
  55.                         msg += e.Exception.ToString();
  56.  
  57.                     }
  58.                     MessageBox.Show(msg, "Dispatcher Unhandled Exception", MessageBoxButton.OK, MessageBoxImage.Information);
  59.                 }
  60.             }
  61.             finally
  62.             {
  63.                 e.Handled = true;
  64.                 try
  65.                 {
  66.                     Process.GetCurrentProcess().Kill();
  67.                 }
  68.                 catch { }
  69.             }
  70.            
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment