using CommonUpdater.VersionControl; using log4net; using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading.Tasks; using System.Windows; namespace PdmAlert { /// /// App.xaml 的交互逻辑 /// public partial class App : Application { public static readonly string AppId = "PdmMsgAlert"; public static readonly string Version = "0.0.0.1"; DockApp dockApp; LoginWindow loginWindow; private static System.Threading.Mutex mutex; private static ILog Logger = LogManager.GetLogger("MsgAlert"); protected override void OnStartup(StartupEventArgs e) { mutex = new System.Threading.Mutex(true, "MsgAlert_OnlyRunOne"); if (mutex.WaitOne(0, false)) { base.OnStartup(e); this.ShutdownMode = ShutdownMode.OnLastWindowClose; // 写入版本,就算异常了也不报错 try { VersionUtil.SetPluginVersion(AppId, Version); } catch (Exception ex) { Logger.Error("Write App Version Failed.", ex); } Task.Run(() => CheckUpdate()); if (LoginUser.TryAutoLogin()) { InitDockApp(); } else { loginWindow = new LoginWindow(); loginWindow.Show(); } } else { MessageBox.Show("请勿多次运行程序!", "提示"); this.Shutdown(); } } public void CheckUpdate() { try { VersionUtil.UpdateAutoUpdater(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); } catch (Exception ex) { Logger.Error("Updater AutoUpdater.exe Failed!", ex); Dispatcher.Invoke(() => { MessageBox.Show("更新程序异常", "错误", MessageBoxButton.OK, MessageBoxImage.Error); }); // 更新程序没有更新,就不检查更新了吧 return; } try { var update = VersionUtil.CheckSingleAppUpdate(AppId, Version); if (update != null) { Dispatcher.Invoke(() => { var res = MessageBox.Show("检查到软件更新,是否现在更新?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question); if (res == MessageBoxResult.Yes) { string updaterPath = "AutoUpdater\\AutoUpdater.exe"; string exePath = new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.FullName; string exeFileName = $"{exePath}\\{updaterPath}"; int mainProcessId = Process.GetCurrentProcess().Id; Process updaterProcess = new Process { StartInfo = new ProcessStartInfo { FileName = exeFileName, Arguments = $"{mainProcessId}" } }; updaterProcess.Start(); } }); } } catch (Exception ex) { Logger.Error($"Check Updater Failed.", ex); Dispatcher.Invoke(() => { MessageBox.Show("检查更新失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); }); } } private void Application_Startup(object sender, StartupEventArgs e) { } public void InitDockApp() { dockApp = new DockApp(); dockApp.Run(); } public void SwithUser() { LoginUser.RemoveCacheBin(); LoginWindow loginWindow = new LoginWindow(); loginWindow.Show(); if (dockApp != null) dockApp.Stop(); } } }