chr
7 天以前 43a0207d207390abdeeb3ab9155eebf03edd7b1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </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)
        {
            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();
        }
    }
}