Chr
2024-08-23 12acdf14fcddae8d16e00b7b981559da67ce60ea
PdmAlert/App.xaml.cs
@@ -1,4 +1,11 @@
using System.Windows;
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
{
@@ -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();
        }
    }
}