protected async override void OnStartup(StartupEventArgs e) { #if !DEBUG //base.OnStartup(e); new WpfCodes.Program.Exception().UnhandledException += (p1, p2) => { try { Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("程序发生了未捕获的错误,类型" + p2.Source.ToString(), p2.Exception)); File.AppendAllText("Exception.log", Environment.NewLine + Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + p2.Exception.ToString()); } catch (Exception ex) { Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("错误信息无法写入", ex)); } finally { App.Current.Shutdown(); } }; if (await WpfCodes.Program.Startup.CheckAnotherInstanceAndOpenWindow <MainWindow>("FileBackuper", this)) { return; } #endif string configFile = "FileBackuperConfig.json"; try { set = Settings.GetJsonSetting <Settings>(configFile); } catch (Exception ex) { WpfControls.Dialog.DialogHelper.ShowException("读取配置文件失败,将重置配置文件", ex, true); set = Settings.CreatJsonSetting <Settings>(configFile); } background = new BackgroundWork(); if (!(e.Args.Length == 1 && e.Args[0] == "noWindow")) { MainWindow = new MainWindow(); MainWindow.Show(); MainWindow.Closed += (p1, p2) => { MainWindow = null; }; } Dictionary <string, Action> trayRightButtonActions = new Dictionary <string, Action>() { { "退出", TryExit } }; tray = new WpfCodes.Program.TrayIcon(FileBackuper.Properties.Resources.Icon, "文件自动备份系统", TrayOpenWindow, trayRightButtonActions); tray.Show(); background.timer.Start(); void TrayOpenWindow() { if (MainWindow == null) { MainWindow = new MainWindow(); MainWindow.Show(); } } }
//private void ShowException(Exception ex,int type) //{ // try // { // Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("程序发生了未捕获的错误,类型"+type.ToString(), ex)); // File.AppendAllText("Exception.log", Environment.NewLine + Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + ex.ToString()); // } // catch (Exception ex2) // { // Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("错误信息无法写入", ex2)); // } // finally // { // App.Current.Shutdown(); // } //} private void ApplicationStartupEventHandler(object sender, StartupEventArgs e) { ConcernConfigPath(); Load(); if (e.Args.Length > 0) { if (e.Args[0] == "noWindow") { startup = true; } else if (e.Args[0] == "readonly") { ReadOnlyMode = true; } } if (!ReadOnlyMode) { #if !DEBUG if (WpfCodes.Program.Startup.HaveAnotherInstance("ComputerUsage")) { WpfControls.Dialog.DialogHelper.ShowError("已存在另一实例,请不要重复运行!"); Environment.Exit(0); return; } //TaskScheduler.UnobservedTaskException += (p1, p2) => { if (!p2.Observed) ShowException(p2.Exception,3); };//Task //AppDomain.CurrentDomain.UnhandledException += (p1, p2) => ShowException((Exception)p2.ExceptionObject,2);//UI //DispatcherUnhandledException += (p1, p2) => ShowException(p2.Exception,1);//Thread new WpfCodes.Program.Exception().UnhandledException += (p1, p2) => { try { Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("程序发生了未捕获的错误,类型" + p2.Source.ToString(), p2.Exception)); File.AppendAllText("Exception.log", Environment.NewLine + Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + p2.Exception.ToString()); } catch (Exception ex) { Dispatcher.Invoke(() => WpfControls.Dialog.DialogHelper.ShowException("错误信息无法写入", ex)); } finally { App.Current.Shutdown(); } }; #endif background = new BackgroundWork(); Dictionary <string, Action> rightMouseClick = new Dictionary <string, Action> { { "回收", () => GC.Collect() }, { "退出", () => Shutdown() }, }; trayIcon = new WpfCodes.Program.TrayIcon(ComputerUsage.Properties.Resources.ICON, "计算机使用情况", newWindow, rightMouseClick); trayIcon.Show(); Task.Run(() => { ComputerDatas.RegistSystemEvents(); }); ClipboardHelper clipBoardHelper = new ClipboardHelper(); ProcessMonitorHelper.Load(); } if (!startup) { newWindow(); } void newWindow() { if (Current.MainWindow as MainWindow == null) { Current.MainWindow = new MainWindow(); Current.MainWindow.Show(); } }; }