From 8666c76f65e34a5a036b3feea0f1956d9a73ba5a Mon Sep 17 00:00:00 2001 From: QI Mingxuan Date: Mon, 11 May 2026 13:27:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=8E=A2=E6=B5=8B=E5=99=A8?= =?UTF-8?q?=E9=80=80=E5=87=BA=E9=98=BB=E5=A1=9E=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E4=B8=AD=E5=8A=A0=E5=85=A5=E8=B6=85=E6=97=B6=E4=BF=9D?= =?UTF-8?q?=E6=8A=A4=EF=BC=8C=E9=81=BF=E5=85=8D=E6=AD=BB=E9=94=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/DetectorService.cs | 73 ++++++++++++++++--- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/XP.Hardware.Detector/Services/DetectorService.cs b/XP.Hardware.Detector/Services/DetectorService.cs index 6f0a564..715d20f 100644 --- a/XP.Hardware.Detector/Services/DetectorService.cs +++ b/XP.Hardware.Detector/Services/DetectorService.cs @@ -570,17 +570,72 @@ namespace XP.Hardware.Detector.Services { if (disposing) { - // 停止采集(如果正在采集)| Stop acquisition if acquiring - StopAcquisitionAsync().GetAwaiter().GetResult(); + // 带超时保护的资源释放,避免退出时无限阻塞 + // Dispose with timeout protection to avoid infinite blocking on exit + const int disposeTimeoutMs = 5000; // 5 秒总超时 | 5 seconds total timeout - // 断开探测器连接 | Disconnect detector - DisconnectAsync().GetAwaiter().GetResult(); - - // 释放托管资源 | Release managed resources - lock (_lock) + try { - _detector?.Dispose(); - _detector = null; + IAreaDetector detector; + lock (_lock) + { + detector = _detector; + } + + if (detector != null) + { + // 仅在正在采集时才停止采集 | Only stop acquisition if currently acquiring + if (detector.Status == DetectorStatus.Acquiring) + { + _logger?.Info("Dispose:探测器正在采集,尝试停止 | Dispose: Detector is acquiring, attempting to stop"); + using var cts = new CancellationTokenSource(disposeTimeoutMs); + var stopTask = Task.Run(async () => await detector.StopAcquisitionAsync(cts.Token), cts.Token); + if (!stopTask.Wait(disposeTimeoutMs)) + { + _logger?.Warn("Dispose:停止采集超时,强制继续释放 | Dispose: Stop acquisition timeout, forcing cleanup"); + } + } + + // 直接释放探测器资源(不再调用 DisconnectAsync 避免重复停止) + // Directly dispose detector resources (skip DisconnectAsync to avoid redundant stop) + lock (_lock) + { + _detector?.Dispose(); + _detector = null; + } + _config = null; + _logger?.Info("Dispose:探测器资源已释放 | Dispose: Detector resources disposed"); + } + } + catch (OperationCanceledException) + { + _logger?.Warn("Dispose:操作已取消,强制释放资源 | Dispose: Operation cancelled, forcing resource cleanup"); + lock (_lock) + { + _detector?.Dispose(); + _detector = null; + } + _config = null; + } + catch (AggregateException ex) + { + _logger?.Warn($"Dispose:聚合异常,强制释放资源 | Dispose: Aggregate exception, forcing cleanup: {ex.InnerException?.Message ?? ex.Message}"); + lock (_lock) + { + _detector?.Dispose(); + _detector = null; + } + _config = null; + } + catch (Exception ex) + { + _logger?.Error(ex, "Dispose:释放探测器资源异常 | Dispose: Exception disposing detector resources"); + lock (_lock) + { + try { _detector?.Dispose(); } catch { /* 忽略 | Ignore */ } + _detector = null; + } + _config = null; } } _disposed = true;