From b5234c5ab1e9e6826b8d8fc1e95fa752aaa40b74 Mon Sep 17 00:00:00 2001
From: chr <chr@mailcom>
Date: 星期六, 02 十一月 2024 10:56:50 +0800
Subject: [PATCH] 优化程序

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

diff --git a/PdmAlert/App.xaml.cs b/PdmAlert/App.xaml.cs
index 67e9db4..dc28055 100644
--- a/PdmAlert/App.xaml.cs
+++ b/PdmAlert/App.xaml.cs
@@ -1,8 +1,9 @@
-锘縰sing System;
-using System.Collections.Generic;
-using System.Configuration;
-using System.Data;
-using System.Linq;
+锘縰sing CommonUpdater.VersionControl;
+using log4net;
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Reflection;
 using System.Threading.Tasks;
 using System.Windows;
 
@@ -13,17 +14,124 @@
     /// </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;
-            dockApp.Run();
+            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();
         }
     }
 }

--
Gitblit v1.9.1