|
| 1 | +using Newtonsoft.Json; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Configuration; |
| 5 | +using System.Data; |
| 6 | +using System.Windows; |
| 7 | +using System.Windows.Threading; |
| 8 | +using GalaSoft.MvvmLight.Messaging; |
| 9 | +using GalaSoft.MvvmLight.Threading; |
| 10 | +using Workshop.Common; |
| 11 | +using Workshop.Helper; |
| 12 | + |
| 13 | + |
| 14 | +namespace Workshop |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// App.xaml 的交互逻辑 |
| 18 | + /// </summary> |
| 19 | + public partial class App : Application |
| 20 | + { |
| 21 | + public static string Session; |
| 22 | + public App() |
| 23 | + { |
| 24 | + App.Current.Startup += Current_Startup; |
| 25 | + App.Current.Exit += Current_Exit; |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + private void Current_Exit(object sender, ExitEventArgs e) |
| 30 | + { |
| 31 | + LogHelper.ExitThread(); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// UI线程抛出全局异常事件处理 |
| 37 | + /// </summary> |
| 38 | + /// <param name="sender"></param> |
| 39 | + /// <param name="e"></param> |
| 40 | + private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) |
| 41 | + { |
| 42 | + try |
| 43 | + { |
| 44 | + Messenger.Default.Send("", MessengerToken.CLOSEPROGRESS); |
| 45 | + |
| 46 | + LogHelper.LogError("UI线程全局异常" + e.Exception); |
| 47 | + MessageBox.Show("An unhandled exception just occurred: " + e.Exception.Message, "UI线程全局异常", MessageBoxButton.OK, MessageBoxImage.Error); |
| 48 | + e.Handled = true; |
| 49 | + } |
| 50 | + catch (Exception ex) |
| 51 | + { |
| 52 | + LogHelper.LogError("不可恢复的UI线程全局异常" + ex); |
| 53 | + MessageBox.Show("An unhandled exception just occurred: " + e.Exception.Message, "不可恢复的UI线程全局异常", MessageBoxButton.OK, MessageBoxImage.Error); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// 非UI线程抛出全局异常事件处理 |
| 59 | + /// </summary> |
| 60 | + /// <param name="sender"></param> |
| 61 | + /// <param name="e"></param> |
| 62 | + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) |
| 63 | + { |
| 64 | + try |
| 65 | + { |
| 66 | + Messenger.Default.Send("", MessengerToken.CLOSEPROGRESS); |
| 67 | + |
| 68 | + var exception = e.ExceptionObject as Exception; |
| 69 | + if (exception != null) |
| 70 | + { |
| 71 | + LogHelper.LogError("非UI线程全局异常" + exception); |
| 72 | + MessageBox.Show("An unhandled exception just occurred: " + exception.Message, "非UI线程全局异常", MessageBoxButton.OK, MessageBoxImage.Error); |
| 73 | + |
| 74 | + } |
| 75 | + } |
| 76 | + catch (Exception ex) |
| 77 | + { |
| 78 | + LogHelper.LogError("不可恢复的非UI线程全局异常" + ex); |
| 79 | + MessageBox.Show("An unhandled exception just occurred: " + ex.Message, "不可恢复的非UI线程全局异常", MessageBoxButton.OK, MessageBoxImage.Error); |
| 80 | + |
| 81 | + } |
| 82 | + } |
| 83 | + private void Current_Startup(object sender, StartupEventArgs e) |
| 84 | + { |
| 85 | + Current.DispatcherUnhandledException += App_OnDispatcherUnhandledException; |
| 86 | + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; |
| 87 | + LogHelper.LogFlag = true; |
| 88 | + DispatcherHelper.Initialize(); |
| 89 | + |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments