From 981923c2a2cff6d61cd5a474d2ba398ba48f1fd5 Mon Sep 17 00:00:00 2001
From: chr <chr@chr.com>
Date: 星期一, 02 九月 2024 17:40:17 +0800
Subject: [PATCH] 更新代码;

---
 PdmAlert/App.xaml.cs |  119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 111 insertions(+), 8 deletions(-)

diff --git a/PdmAlert/App.xaml.cs b/PdmAlert/App.xaml.cs
index 9740dfb..dc28055 100644
--- a/PdmAlert/App.xaml.cs
+++ b/PdmAlert/App.xaml.cs
@@ -1,4 +1,11 @@
-锘縰sing System.Windows;
+锘縰sing CommonUpdater.VersionControl;
+using log4net;
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Reflection;
+using System.Threading.Tasks;
+using System.Windows;
 
 namespace PdmAlert
 {
@@ -7,22 +14,104 @@
     /// </summary>
     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)
         {
-            base.OnStartup(e);
-            this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
-            if (LoginUser.TryAutoLogin())
+            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);
+                }
 
-                dockApp = new DockApp();
-                dockApp.Run();
+
+                Task.Run(() => CheckUpdate());
+
+
+                if (LoginUser.TryAutoLogin())
+                {
+                    InitDockApp();
+                }
+                else
+                {
+                    loginWindow = new LoginWindow();
+                    loginWindow.Show();
+                }
             }
             else
             {
-                loginWindow = new LoginWindow();
-                loginWindow.Show();
+                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);
+                });
             }
         }
 
@@ -30,5 +119,19 @@
         {
 
         }
+
+        public void InitDockApp()
+        {
+            dockApp = new DockApp();
+            dockApp.Run();
+        }
+
+        public void SwithUser()
+        {
+            LoginUser.RemoveCacheBin();
+            LoginWindow loginWindow = new LoginWindow();
+            loginWindow.Show();
+            if (dockApp != null) dockApp.Stop();
+        }
     }
 }

--
Gitblit v1.9.1