From fa76bdb61c0c7dcffb85e3dfc44e3bcefdd9f410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chsc=E2=80=9D?= <“huangsucan@kaiyili-lab.com”> Date: Thu, 10 Sep 2026 14:33:40 +0800 Subject: [PATCH] =?UTF-8?q?P3:=20DeviceHealthMonitor=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=20DeviceInfoVM.IsConnected=20=E6=94=B9=E7=94=A8=20Dispatcher.B?= =?UTF-8?q?eginInvoke=20=E5=88=87=E5=9B=9EUI=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UIShare/GlobalVariable/DeviceHealthMonitor.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/UIShare/GlobalVariable/DeviceHealthMonitor.cs b/UIShare/GlobalVariable/DeviceHealthMonitor.cs index 573610e..b95d497 100644 --- a/UIShare/GlobalVariable/DeviceHealthMonitor.cs +++ b/UIShare/GlobalVariable/DeviceHealthMonitor.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using System.Windows; namespace UIShare.GlobalVariable { @@ -221,17 +222,27 @@ namespace UIShare.GlobalVariable } /// - /// 同步更新 SystemConfig.DeviceList 中对应设备的 IsConnected 状态(驱动 UI 刷新) + /// 通过 UI 线程更新 SystemConfig.DeviceList 中对应设备的 IsConnected 状态(驱动 UI 刷新)。 + /// Timer 回调运行在线程池线程上,必须切回 UI 线程才能触发 PropertyChanged。 /// private void UpdateDeviceInfoState(string deviceName, bool isConnected) { var info = _systemConfig?.DeviceList? .FirstOrDefault(d => d != null && string.Equals(d.DeviceName, deviceName, StringComparison.OrdinalIgnoreCase)); - if (info != null) + if (info == null) return; + + var dispatcher = Application.Current?.Dispatcher; + if (dispatcher == null || dispatcher.CheckAccess()) { + // 已在 UI 线程(或无 Dispatcher),直接赋值 info.IsConnected = isConnected; } + else + { + // 切回 UI 线程赋值,避免跨线程 PropertyChanged 异常 + dispatcher.BeginInvoke(() => info.IsConnected = isConnected); + } } public void Dispose()