C3031
2026-01-30 130802fa7ef10857db12f784956d5ea122a1eeb4
LB_SmartVision/VisionForm.cs
@@ -3,19 +3,23 @@
using LB_SmartVision.Forms.Pages;
using LB_SmartVision.Forms.Pages.CameraPage;
using LB_SmartVision.Forms.Pages.CommunicatorPage;
using LB_SmartVision.Forms.Pages.HistoricalData;
using LB_SmartVision.Forms.Pages.MESPage;
using LB_SmartVision.Forms.Pages.MotionControlPage;
using LB_SmartVision.Forms.Pages.ProcessPage;
using LB_SmartVision.Forms.Pages.SettingPage;
using LB_SmartVision.Forms.Pages.UserManagementPage;
using LB_SmartVision.ProcessRun;
using LB_SmartVision.SQL;
using LB_SmartVision.Tool;
using LB_SmartVisionCommon;
using LB_SmartVisionLoginUI;
using LB_VisionProcesses;
using LB_VisionProcesses.Cameras;
using LB_VisionProcesses.Cameras.HRCameras;
using LB_VisionProcesses.Cameras.LBCameras;
using LB_VisionProcesses.Communicators;
using LB_VisionProcesses.Communicators.SiemensS7;
using LB_VisionProcesses.Communicators.TCom;
using LB_VisionProcesses.Forms;
using log4net.Config;
@@ -42,6 +46,7 @@
    {
        AllProcessesPage AllProcessesPages = new AllProcessesPage();
        CamerasEditPage CamerasEditPage = new CamerasEditPage();
        HistoricalDataEditPage HistoricalDataEditPage = new HistoricalDataEditPage();
        CommunicatorsEditPage CommunicatorsEditPage = new CommunicatorsEditPage();
        SettingEditPage SettingEditPage = new SettingEditPage();
        MESEditPage MESEditPage = new MESEditPage();
@@ -62,6 +67,7 @@
            Assembly_LB_VisionProcessesDll = Assembly.Load(Assembly_LB_VisionProcessesBytes);
            GlobalVar.dicCommunicators.DictionaryChanged += CommunicatorsChanged;
            GlobalVar.dicCameras.DictionaryChanged += CamerasChanged;
            GlobalVar.dicProcesses.DictionaryChanged += ProcessRunBllChanged;
            //最开始就清空所有Tab页
@@ -69,6 +75,8 @@
            AllProcessesPages.controlsPanel.Dock = DockStyle.Fill;
            materialTabControl.Controls.Add(AllProcessesPages);
            HistoricalDataEditPage.LogInfo += LogInfo;
            materialTabControl.Controls.Add(new MyPage(HistoricalDataEditPage));
            CamerasEditPage.LogInfo += LogInfo;
            materialTabControl.Controls.Add(new MyPage(CamerasEditPage));
@@ -233,7 +241,30 @@
        private void CommunicatorsChanged(object? sender, DictionaryChangedEventArgs<string, BaseCommunicator> e)
        {
            try
            {
                switch (e.ChangeType)
                {
                    case DictionaryChangeType.Added:
                        e.NewValue.TriggerRunMessageReceived += TriggerRunMessageReceived;
                        LogInfo($"通讯口[{e.NewValue.CommunicatorName}]加载触发通讯", LogInfoType.INFO);
                        e.NewValue.CommunicatorName = e.NewKey;
                        break;
                    case DictionaryChangeType.Renamed:
                        string OldCommunicatorName = e.OldKey;
                        string NewCommunicatorName = e.NewKey;
                        LogInfo(string.Format("重命名通讯口名[{0}]修改为[{1}]", OldCommunicatorName, NewCommunicatorName), LogInfoType.INFO);
                        e.NewValue.CommunicatorName = NewCommunicatorName;
                        break;
                    case DictionaryChangeType.Removed:
                        if (e.OldValue != null && e.OldValue is BaseCommunicator)
                            e.OldValue.Disconnect();
                        e.OldValue.TriggerRunMessageReceived -= TriggerRunMessageReceived;
                        LogInfo($"通讯口[{e.OldValue.CommunicatorName}]移除触发通讯", LogInfoType.INFO);
                        break;
                }
            }
            catch { }
        }
        private void LogInfo(string strLog, LogInfoType infoType)
@@ -242,7 +273,7 @@
            {
                return;
            }
            string strInfo = DateTime.Now.ToString("[yyyy:MM:HH:mm:ss:fff] ");
            string strInfo = DateTime.Now.ToString("[yyyy:MM:dd:HH:mm:ss:fff] ");
            strInfo += strLog;
            if (infoType != LogInfoType.NOSHOW)
            {
@@ -258,19 +289,30 @@
                        switch (infoType)
                        {
                            case LogInfoType.INFO:
                                this.rich_Info.SelectionColor = Color.Wheat;
                                break;
                                {
                                    this.rich_Info.SelectionColor = Color.Wheat;
                                    AsyncLogHelper.Info(strLog);
                                    break;
                                }
                            case LogInfoType.WARN:
                                this.rich_Info.SelectionColor = Color.LightGoldenrodYellow;
                                break;
                                {
                                    this.rich_Info.SelectionColor = Color.Yellow;
                                    AsyncLogHelper.Warn(strLog);
                                    break;
                                }
                            case LogInfoType.PASS:
                                this.rich_Info.SelectionColor = Color.Green;
                                break;
                                {
                                    this.rich_Info.SelectionColor = Color.Green;
                                    AsyncLogHelper.Info(strLog);
                                    break;
                                }
                            case LogInfoType.ERROR:
                                this.rich_Info.SelectionColor = Color.Red;
                                break;
                                {
                                    this.rich_Info.SelectionColor = Color.Red;
                                    AsyncLogHelper.Error(strLog);
                                    break;
                                }
                        }
                        // 更新 UI 控件,比如显示接收到的消息
                        this.rich_Info.AppendText(strInfo);
                        this.rich_Info.AppendText("\r\n");
@@ -282,32 +324,43 @@
                {
                    if (this.rich_Info.Lines.Length > 1000)
                    {
                        this.rich_Info.Clear();
                    }
                    // 如果已经在 UI 线程上,直接更新 UI
                    switch (infoType)
                    {
                        case LogInfoType.INFO:
                            this.rich_Info.SelectionColor = Color.Wheat;
                            break;
                            {
                                this.rich_Info.SelectionColor = Color.Wheat;
                                AsyncLogHelper.Info(strLog);
                                break;
                            }
                        case LogInfoType.WARN:
                            this.rich_Info.SelectionColor = Color.Yellow;
                            break;
                            {
                                this.rich_Info.SelectionColor = Color.Yellow;
                                AsyncLogHelper.Warn(strLog);
                                break;
                            }
                        case LogInfoType.PASS:
                            this.rich_Info.SelectionColor = Color.Green;
                            break;
                            {
                                this.rich_Info.SelectionColor = Color.Green;
                                AsyncLogHelper.Info(strLog);
                                break;
                            }
                        case LogInfoType.ERROR:
                            this.rich_Info.SelectionColor = Color.Red;
                            break;
                            {
                                this.rich_Info.SelectionColor = Color.Red;
                                AsyncLogHelper.Error(strLog);
                                break;
                            }
                    }
                    this.rich_Info.AppendText(strInfo);
                    this.rich_Info.AppendText("\r\n");
                    this.rich_Info.SelectionStart = this.rich_Info.Text.Length;
                    this.rich_Info.ScrollToCaret();
                }
            }
            AsyncLogHelper.Info(strLog);
        }
        public static bool SaveAllLayout()
@@ -498,13 +551,35 @@
            }
        }
        private void CamerasChanged(object sender, DictionaryChangedEventArgs<string, BaseCamera> e)
        {
            try
            {
                switch (e.ChangeType)
                {
                    case DictionaryChangeType.Added:
                        e.NewValue.TriggerRunMessageReceived += TriggerRunMessageReceived;
                        LogInfo($"相机[{e.NewValue.SN}]加载触发通讯", LogInfoType.INFO);
                        e.NewValue.SN = e.NewKey;
                        break;
                    case DictionaryChangeType.Removed:
                        if (e.OldValue != null && e.OldValue is BaseCommunicator)
                            e.OldValue.CloseDevice();
                        e.OldValue.TriggerRunMessageReceived -= TriggerRunMessageReceived;
                        LogInfo($"相机[{e.OldValue.SN}]移除触发通讯", LogInfoType.INFO);
                        break;
                }
            }
            catch { }
        }
        private void VisionForm_Load(object sender, EventArgs e)
        {
            XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
            string[] paths = {
            @"生产日志\Run_Log",
            @"生产日志\Debug_Log",
            @"生产日志\Error_Log",
            @"生产日志\Run",
            @"生产日志\Debug",
            @"生产日志\Error",
            @"生产日志\Fatal",
            @"生产日志\Warn",
            };
@@ -559,7 +634,15 @@
            {
                LogInfo("全局变量加载失败", LogInfoType.ERROR);
            }
            GlobalVar.dicMotionControlData.Clear();
            if (LoadMotionControlDatas(GlobalVar.allMotionControlDataPath))
            {
                LogInfo("运动控制参数加载成功", LogInfoType.PASS);
            }
            else
            {
                LogInfo("运动控制参数加载失败", LogInfoType.ERROR);
            }
            //加载流程
            GlobalVar.dicProcesses.Clear();
            if (LoadAllProcess(GlobalVar.allProcessConnectionStringPath))
@@ -613,6 +696,7 @@
            com_ProductName.Items.Add("新增");
            com_ProductName.Text = GlobalVar.strProductName;
            this.WindowState = FormWindowState.Maximized;
            DatabaseRecordProductDataHelper.InitializeDatabase();
        }
        public void SaveAllSetting()
@@ -624,6 +708,54 @@
            SaveAllProcessSetting();
            SaveAllLayout();
            SaveAllCsv();
            SaveMotionControlDatas();
        }
        public bool LoadMotionControlDatas(string alMotionControlDataPath)
        {
            try
            {
                GlobalVar.dicMotionControlData = ConfigManager<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>.LoadConfig<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>(alMotionControlDataPath);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool SaveMotionControlDatas()
        {
            try
            {
                ConcurrentDictionary<string, List<string>> removeCameraSN = new ConcurrentDictionary<string, List<string>>();
                foreach (var item in GlobalVar.dicMotionControlData.Keys)
                {
                    List<string> list = new List<string>();
                    foreach (var itemSN in GlobalVar.dicMotionControlData[item].Keys)
                    {
                        if (!GlobalVar.dicCameras.Keys.Contains(itemSN))
                        {
                            list.Add(itemSN);
                        }
                    }
                    if (list.Count > 0)
                    {
                        removeCameraSN.TryAdd(item, list);
                    }
                }
                foreach (var item in removeCameraSN.Keys)
                {
                    foreach (var itemSN in removeCameraSN[item])
                    {
                        GlobalVar.dicMotionControlData[item].Keys.Remove(itemSN);
                    }
                }
                ConfigManager<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>.SaveConfig<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>(GlobalVar.dicMotionControlData, GlobalVar.allMotionControlDataPath);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool LoadAllCameras(string allCamerasConnectionStringPath)
@@ -661,7 +793,7 @@
                        }
                    case CameraBrand.LBCamera:
                        {
                            //camera = new LBCamera();
                            camera = new LBCamera();
                            break;
                        }
                    default:
@@ -671,7 +803,7 @@
                        }
                }
                camera.SN = CameraConnectionString.Key;
                if (!camera.InitDevice(CameraConnectionString.Key, this.Handle))
                if (!camera.InitDevice(CameraConnectionString.Key, IntPtr.Zero))
                {
                    LogInfo($"初始化相机[{CameraConnectionString.Key}]失败", LogInfoType.ERROR);
                    if (camera != null)
@@ -764,65 +896,121 @@
                {
                    string CommunicatorName = CommunicatorConnectionString.Key;
                    string CommunicatorAddress = CommunicatorConnectionString.Value;
                    // 定义正则表达式以提取协议、IP 地址和端口
                    //1.    \((.*?)\):\(和 \) 是用于匹配括号的转义字符。
                    //      (.*?) 是一个非贪婪的匹配,用来匹配类名(MyProcesses.Communicators.TCPServer 或 MyProcesses.Communicators.UARTPort)。
                    //2.    ([^:] +):匹配冒号之前的部分,即地址(127.0.0.1 或 COM5)。这里使用了[^:] 来匹配除了冒号之外的任意字符。
                    //3.    (\d +) :匹配端口号,确保它匹配一个或多个数字。
                    string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<PORT>[^]]+)\]$";
                    Match match = Regex.Match(CommunicatorAddress, pattern);
                    if (match.Success)
                    if (!string.IsNullOrEmpty(CommunicatorAddress) && CommunicatorAddress.Contains("SiemensLBS7"))
                    {
                        string ClassName = match.Groups["ClassName"].Value;   // "TCP"
                        string IP = match.Groups["IP"].Value;          // "127.0.0.1"
                        string PORT = match.Groups["PORT"].Value;        // "1111"
                        // 定义正则表达式以提取协议、IP 地址和端口
                        // 更新正则以支持可选的数据类型字段
                        string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<Slot>[^]]+)\]\[(?<CpuType>[^]]+)\]\[(?<PlcAddress>[^]]+)\](?:\[(?<DataType>[^]]+)\])?$";
                        Match match = Regex.Match(CommunicatorAddress, pattern);
                        if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
                            break;
                        //利用反射创建实例
                        Type type = IProcess.GetExecutingAssembly().GetType(ClassName);
                        if (type == null)
                        if (match.Success)
                        {
                            Debug.WriteLine("Class not found.");
                            return false;
                        }
                        var Communicator = Activator.CreateInstance(type, CommunicatorName) as BaseCommunicator;
                            string ClassName = match.Groups["ClassName"].Value;   // "TCP"
                            string IP = match.Groups["IP"].Value;          // "127.0.0.1"
                            string Slot = match.Groups["Slot"].Value;        // "1111"
                            string CpuType = match.Groups["CpuType"].Value;
                            string PlcAddress = match.Groups["PlcAddress"].Value;
                            string DataType = match.Groups["DataType"].Success ? match.Groups["DataType"].Value : "String";
                        if (Communicator == null)
                        {
                            Debug.WriteLine("BaseCommunicator not found.");
                            return false;
                        }
                            if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(Slot) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress))
                                break;
                        //TCP客户端最后再连接
                        if (Communicator is TCPClient)
                        {
                            clientsCommunicatorsConnectionString.TryAdd(CommunicatorConnectionString.Key, CommunicatorConnectionString.Value);
                            continue;
                        }
                            //利用反射创建实例
                            Type type = IProcess.GetExecutingAssembly().GetType(ClassName);
                            if (type == null)
                            {
                                Debug.WriteLine("Class not found.");
                                return false;
                            }
                            var Communicator = Activator.CreateInstance(type, CommunicatorName) as BaseCommunicator;
                        Communicator.CommunicatorConnections.Add("地址", IP);
                        Communicator.CommunicatorConnections.Add("端口", PORT);
                        Communicator.CommunicatorName = CommunicatorName;
                        if (!Communicator.Connect())
                        {
                            LogInfo($"初始化通讯口[{CommunicatorName}]失败,原因是{Communicator.Msg}", LogInfoType.ERROR);
                            if (Communicator == null)
                            {
                                Debug.WriteLine("BaseCommunicator not found.");
                                return false;
                            }
                            Communicator.CommunicatorConnections.Add("地址", IP);
                            Communicator.CommunicatorConnections.Add("端口", Slot);
                            Communicator.CommunicatorConnections.Add("型号", CpuType);
                            Communicator.CommunicatorConnections.Add("变量地址", PlcAddress);
                            Communicator.CommunicatorConnections.Add("数据类型", DataType);
                            Communicator.CommunicatorName = CommunicatorName;
                            if (!Communicator.Connect())
                            {
                                LogInfo($"初始化通讯口[{CommunicatorName}]失败,原因是{Communicator.Msg}", LogInfoType.ERROR);
                            }
                            else
                            {
                                LogInfo($"初始化通讯口[{CommunicatorName}]成功", LogInfoType.PASS);
                            }
                            GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
                        }
                        else
                        {
                            LogInfo($"初始化通讯口[{CommunicatorName}]成功", LogInfoType.PASS);
                            Debug.WriteLine("No match found.");
                        }
                        GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
                    }
                    else
                    {
                        Debug.WriteLine("No match found.");
                    }
                        // 定义正则表达式以提取协议、IP 地址和端口
                        //1.    \((.*?)\):\(和 \) 是用于匹配括号的转义字符。
                        //      (.*?) 是一个非贪婪的匹配,用来匹配类名(MyProcesses.Communicators.TCPServer 或 MyProcesses.Communicators.UARTPort)。
                        //2.    ([^:] +):匹配冒号之前的部分,即地址(127.0.0.1 或 COM5)。这里使用了[^:] 来匹配除了冒号之外的任意字符。
                        //3.    (\d +) :匹配端口号,确保它匹配一个或多个数字。
                        string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<PORT>[^]]+)\]$";
                        Match match = Regex.Match(CommunicatorAddress, pattern);
                        if (match.Success)
                        {
                            string ClassName = match.Groups["ClassName"].Value;   // "TCP"
                            string IP = match.Groups["IP"].Value;          // "127.0.0.1"
                            string PORT = match.Groups["PORT"].Value;        // "1111"
                            if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
                                break;
                            //利用反射创建实例
                            Type type = IProcess.GetExecutingAssembly().GetType(ClassName);
                            if (type == null)
                            {
                                Debug.WriteLine("Class not found.");
                                return false;
                            }
                            var Communicator = Activator.CreateInstance(type, CommunicatorName) as BaseCommunicator;
                            if (Communicator == null)
                            {
                                Debug.WriteLine("BaseCommunicator not found.");
                                return false;
                            }
                            //TCP客户端最后再连接
                            if (Communicator is TCPClient)
                            {
                                clientsCommunicatorsConnectionString.TryAdd(CommunicatorConnectionString.Key, CommunicatorConnectionString.Value);
                                continue;
                            }
                            Communicator.CommunicatorConnections.Add("地址", IP);
                            Communicator.CommunicatorConnections.Add("端口", PORT);
                            Communicator.CommunicatorName = CommunicatorName;
                            if (!Communicator.Connect())
                            {
                                LogInfo($"初始化通讯口[{CommunicatorName}]失败,原因是{Communicator.Msg}", LogInfoType.ERROR);
                            }
                            else
                            {
                                LogInfo($"初始化通讯口[{CommunicatorName}]成功", LogInfoType.PASS);
                            }
                            GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
                        }
                        else
                        {
                            Debug.WriteLine("No match found.");
                        }
                    }
                }
                //TCP客户端最后连接
@@ -897,19 +1085,35 @@
            {
                string strJson = string.Empty;
                GlobalVar.allCommunicatorsConnectionString = new ConcurrentDictionary<string, string>();
                foreach (var item in GlobalVar.dicCommunicators)
                {
                    string ClassName = item.Value.GetType().FullName;// "TCP"
                    string IP = item.Value.CommunicatorConnections["地址"].ToString();//"127.0.0.1"
                    string PORT = item.Value.CommunicatorConnections["端口"].ToString();//"1111"
                    if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
                    if (!string.IsNullOrEmpty(ClassName) && ClassName.Contains("SiemensLBS7"))
                    {
                        break;
                        string CpuType = item.Value.CommunicatorConnections["型号"].ToString();
                        string PlcAddress = item.Value.CommunicatorConnections["变量地址"].ToString();
                        string DataType = item.Value.CommunicatorConnections.Contains("数据类型") ?
                                          item.Value.CommunicatorConnections["数据类型"].ToString() : "String";
                        if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress))
                        {
                            break;
                        }
                        string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}][{CpuType}][{PlcAddress}][{DataType}]";
                        GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
                    }
                    string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}]";
                    GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
                    else
                    {
                        if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
                        {
                            break;
                        }
                        string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}]";
                        GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
                    }
                    GlobalVar.dicCommunicators[item.Key].ClassName = ClassName;
                }
                var settings = new JsonSerializerSettings
                {
@@ -934,6 +1138,11 @@
                    { }
                }
                File.WriteAllText(GlobalVar.allCommunicatorsConnectionStringPath, strJson, Encoding.UTF8);
                foreach (var item in GlobalVar.dicCommunicators)
                {
                    string ClassName = item.Value.GetType().FullName;// "TCP"
                }
                ConfigManager<ObservableConcurrentDictionary<string, BaseCommunicator>>.SaveConfig<ObservableConcurrentDictionary<string, BaseCommunicator>>(GlobalVar.dicCommunicators, GlobalVar.strApplicationPath + "\\所有产品\\" + GlobalVar.strProductName + "\\dicCommunicators.json");
                return true;
            }
            catch { return false; }
@@ -1193,6 +1402,7 @@
                GlobalVar.strProductName = com_ProductName.SelectedItem?.ToString();
                foreach (BaseCamera camera in GlobalVar.dicCameras.Values)
                {
                    camera.TriggerRunMessageReceived -= TriggerRunMessageReceived;
                    camera.Dispose();
                }
                GlobalVar.dicCameras.Clear();
@@ -1506,6 +1716,128 @@
                communicator.Disconnect();
            }
            FormClosing -= VisionForm_FormClosing;
            //try
            //{
            //    Process[] processes = System.Diagnostics.Process.GetProcesses(); //获得所有进程
            //    foreach (Process p in processes)
            //    {
            //        if (p.ProcessName == "LB_SmartVision" && p.StartTime < DateTime.Now.AddMilliseconds(-300))
            //        {
            //            p.Kill();
            //        }
            //    }
            //}
            //catch { }
            KillAllTargetProcesses();
        }
        /// <summary>
        /// 杀进程逻辑
        /// </summary>
        private void KillAllTargetProcesses()
        {
            try
            {
                // 1. 获取当前进程ID,避免杀死自己
                int currentProcessId = Process.GetCurrentProcess().Id;
                // 2. 模糊匹配进程名(忽略大小写),覆盖vshost/后缀等情况
                var targetProcesses = Process.GetProcesses()
                    .Where(p =>
                        p.ProcessName.IndexOf("LB_SmartVision", StringComparison.OrdinalIgnoreCase) >= 0
                        && p.Id == currentProcessId);
                foreach (var p in targetProcesses)
                {
                    try
                    {
                        if (!p.HasExited)
                        {
                            // 先尝试优雅关闭WinForm程序(比直接Kill更友好)
                            p.CloseMainWindow();
                            // 等待500ms,看是否正常退出
                            if (!p.WaitForExit(500))
                            {
                                p.Kill(); // 强制杀死
                                p.WaitForExit(1000); // 等待进程完全终止
                            }
                        }
                        p.Dispose(); // 释放进程资源,避免句柄泄漏
                    }
                    catch (Exception ex)
                    {
                        // 记录异常但不中断,比如权限不足的情况
                        System.IO.File.AppendAllText("kill_process_log.txt", $"[{DateTime.Now}] 杀死进程失败: {ex.Message}\r\n");
                    }
                }
                // 额外延迟,确保文件句柄完全释放
                Thread.Sleep(1000);
            }
            catch (Exception)
            {
                // 静默失败,不影响程序退出
            }
        }
        private void btn_Run_Click(object sender, EventArgs e)
        {
            var matchedItems = GlobalVar.dicProcessSetting
                                        .Where(item =>
                                        {
                                            var value = item.Value;
                                            var triggerComm = value["触发通讯"];
                                            var triggerChar = value["触发字符"];
                                            return triggerComm != null;
                                            //return triggerComm != null && triggerComm.Equals(name) &&
                                            //       (string.IsNullOrEmpty(triggerChar?.ToString()) ||
                                            //        msg.StartsWith(triggerChar.ToString()));
                                        })
                                        .ToList(); // 避免重复字典访问和装箱操作
            if (matchedItems.Count <= 0)
            {
                return;
            }
            TriggerRunMessageReceived(matchedItems[0].Value["触发通讯"], matchedItems[0].Value["触发字符"]);
        }
        private void btn_RunContinuously_Click(object sender, EventArgs e)
        {
            if (btn_RunContinuously.Text.Equals("连续运行"))
            {
                btn_RunContinuously.Text = "暂停运行";
                Task.Factory.StartNew(() =>
                {
                    var matchedItems = GlobalVar.dicProcessSetting
                                                .Where(item =>
                                                {
                                                    var value = item.Value;
                                                    var triggerComm = value["触发通讯"];
                                                    var triggerChar = value["触发字符"];
                                                    return triggerComm != null;
                                                    //return triggerComm != null && triggerComm.Equals(name) &&
                                                    //       (string.IsNullOrEmpty(triggerChar?.ToString()) ||
                                                    //        msg.StartsWith(triggerChar.ToString()));
                                                })
                                                .ToList(); // 避免重复字典访问和装箱操作
                    while (btn_RunContinuously.Text.Equals("暂停运行"))
                    {
                        TriggerRunMessageReceived(matchedItems[0].Value["触发通讯"], matchedItems[0].Value["触发字符"]);
                        Thread.Sleep(500);
                    }
                    //this.Invoke(() =>
                    //{
                    //});
                });
            }
            else if (btn_RunContinuously.Text.Equals("暂停运行"))
            {
                btn_RunContinuously.Text = "连续运行";
                Thread.Sleep(100);
            }
        }
    }
}