From 559890c482f5973f06aafdfec9550fee5ba3c7f2 Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 09 十一月 2026 17:02:15 +0800
Subject: [PATCH] feat: CAN 模拟器 TCP 化 + 多设备并发调试 + 后台生命周期管理

---
 WPFProject/App.xaml.cs |  146 +++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 136 insertions(+), 10 deletions(-)

diff --git a/WPFProject/App.xaml.cs b/WPFProject/App.xaml.cs
index 437e4c9..4a335ed 100644
--- a/WPFProject/App.xaml.cs
+++ b/WPFProject/App.xaml.cs
@@ -1,10 +1,19 @@
-锘縰sing System;
-using System.Collections.Generic;
-using System.Configuration;
-using System.Data;
-using System.Linq;
-using System.Threading.Tasks;
+锘縰sing HMI.Core.Setting;
+using HMI.Core.Util;
+using HMI.UI.Util;
+using log4net;
+using Microsoft.Win32;
+using OpenTap.Addin.Util;
+using Panuon.WPF.UI;
+using Plugin.TsSupport;
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Management;
+using System.Reflection;
+using System.Threading;
 using System.Windows;
+using WPFProject.Utils;
 
 namespace WPFProject
 {
@@ -13,15 +22,132 @@
     /// </summary>
     public partial class App : Application
     {
-        public void test()
+        private ILog log;
+        private LogCleanner logCleanner;
+        public App()
         {
-           
+            log = LogManager.GetLogger("APP");
+            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
         }
 
+        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
+        {
+            var ex = e.ExceptionObject as Exception;
+            log.Error("UnhandledException.", ex);
+            MsgBoxHelper.ErrorMessage(Current.MainWindow, this.lang("App.AppUnhandledException", ex.Message),
+                    this.lang("App.Error"), MessageBoxButton.OK,
+                    MessageBoxIcon.Error, DefaultButton.YesOK, 5);
+        }
+
+        public static Process GetProcess()
+        {
+            Process cur = Process.GetCurrentProcess();
+            string exePath = Assembly.GetExecutingAssembly().Location;
+            string query = $"SELECT ProcessId FROM Win32_Process WHERE ExecutablePath = '{exePath.Replace("\\", "\\\\")}'";
+            using (var searcher = new ManagementObjectSearcher(query))
+            {
+                foreach (var obj in searcher.Get())
+                {
+                    int processId = Convert.ToInt32(obj["ProcessId"]);
+                    if (processId != cur.Id)
+                    {
+                        return Process.GetProcessById(processId);
+                    }
+                }
+            }
+            return null;
+        }
+
+        /// <summary>
+        /// 鍚姩鍚庡線娉ㄥ唽琛ㄥ啓鐐逛笢瑗�
+        /// </summary>
+        private void WriteRegister()
+        {
+            string Company = "JingCe", Program = "HMI";
+            try
+            {
+                string registryPath = @$"SOFTWARE\{Company}\{Program}";
+
+                using RegistryKey key = Registry.CurrentUser.CreateSubKey(registryPath, true)
+                    ?? throw new Exception("鏃犳硶鍒涘缓鎴栨墦寮�娉ㄥ唽琛ㄩ敭");
+                string path = AppDomain.CurrentDomain.BaseDirectory;
+                key.SetValue("Location", path, RegistryValueKind.String);
+            }
+            catch (Exception ex)
+            {
+                log.Error("Write Register Failed.", ex);
+            }
+        }
+
+        private void DoUpdateAction()
+        {
+            using (SqliteHelper helper = new SqliteHelper(PathHelper.DatabasePath))
+            {
+                if (!helper.HasData("SELECT 1 FROM pragma_table_info('user') WHERE name = 'code';"))
+                {
+                    helper.ExecuteSql("ALTER TABLE user ADD code TEXT;");
+                }
+            }
+        }
+
+        private static Mutex mutex;
         private void Application_Startup(object sender, StartupEventArgs e)
         {
-            MainWindow window = new MainWindow();
-            window.Show();
+            var process = GetProcess();
+
+            mutex = new Mutex(true, "TestPlatform_OnlyRun_CRNS__");
+            if (mutex.WaitOne(0, false))//&& process == null
+            {
+                RegUtil.Register(new Net47Getter());
+                WriteRegister();
+                try
+                {
+                    DoUpdateAction();
+                }
+                catch (Exception ex) {
+                    log.Error("Update Failed.", ex);
+                }
+
+                logCleanner = new LogCleanner();
+                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
+                LoggerHelper.Register(new CommonLogger());
+                LangUtil.Load();
+                SystemSetting.SystemSettingChanged += SystemSetting_SystemSettingChanged;
+                ToastHelper.Register(new MainToastHandler());
+                //LoadingWindow window = new LoadingWindow();
+                Login window = new Login();
+                window.Show();
+            }
+            else
+            {
+                MessageBox.Show("绋嬪簭宸茬粡鍦ㄨ繍琛岋紒", "鎻愮ず");
+                this.Shutdown();
+            }
+        }
+
+        private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
+        {
+            // 鑾峰彇璇锋眰鍔犺浇鐨勭▼搴忛泦鍚嶇О
+            string assemblyName = new AssemblyName(args.Name).Name;
+
+            string[] dirs = new string[] { "3rds", "Libs" };
+
+            foreach (string dir in dirs)
+            {
+                string dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dir, $"{assemblyName}.dll");
+                if (File.Exists(dllPath))
+                {
+                    // 濡傛灉渚濊禆 DLL 瀛樺湪锛屽垯鍔犺浇骞惰繑鍥炲畠
+                    return Assembly.LoadFrom(dllPath);
+                }
+            }
+            // 鍚﹀垯杩斿洖 null
+            return null;
+        }
+
+        private void SystemSetting_SystemSettingChanged(object sender, SystemSetting e)
+        {
+            LangUtil.LoadLang();
         }
     }
 }

--
Gitblit v1.9.1