From be16e1a5d748c8b9b1168dbae7fce2114923bd95 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sat, 6 Sep 2025 19:59:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=80=80=E5=87=BA=E6=97=B6?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E6=89=80=E6=9C=89=E6=9C=8D=E5=8A=A1=E5=92=8C?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/IMqttBackgroundService.cs | 2 +- .../Services/IMqttServiceManager.cs | 2 +- .../Services/MqttBackgroundService.cs | 15 +++++ DMS.WPF/App.xaml.cs | 60 +++++++++++++++++-- 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/DMS.Infrastructure/Interfaces/Services/IMqttBackgroundService.cs b/DMS.Infrastructure/Interfaces/Services/IMqttBackgroundService.cs index 811ff3a..b7c9f0b 100644 --- a/DMS.Infrastructure/Interfaces/Services/IMqttBackgroundService.cs +++ b/DMS.Infrastructure/Interfaces/Services/IMqttBackgroundService.cs @@ -9,7 +9,7 @@ namespace DMS.Infrastructure.Interfaces.Services /// /// MQTT后台服务接口,负责管理MQTT连接和数据传输 /// - public interface IMqttBackgroundService + public interface IMqttBackgroundService : IDisposable { /// /// 启动MQTT后台服务 diff --git a/DMS.Infrastructure/Interfaces/Services/IMqttServiceManager.cs b/DMS.Infrastructure/Interfaces/Services/IMqttServiceManager.cs index 989b342..58b604d 100644 --- a/DMS.Infrastructure/Interfaces/Services/IMqttServiceManager.cs +++ b/DMS.Infrastructure/Interfaces/Services/IMqttServiceManager.cs @@ -9,7 +9,7 @@ namespace DMS.Infrastructure.Interfaces.Services /// /// MQTT服务管理器接口,负责管理MQTT连接和变量监控 /// - public interface IMqttServiceManager + public interface IMqttServiceManager : IDisposable { /// /// 初始化服务管理器 diff --git a/DMS.Infrastructure/Services/MqttBackgroundService.cs b/DMS.Infrastructure/Services/MqttBackgroundService.cs index 1a8bc34..3167e48 100644 --- a/DMS.Infrastructure/Services/MqttBackgroundService.cs +++ b/DMS.Infrastructure/Services/MqttBackgroundService.cs @@ -243,5 +243,20 @@ namespace DMS.Infrastructure.Services _logger.LogInformation("MQTT列表发生了变化,正在重新加载数据..."); _reloadSemaphore.Release(); } + + /// + /// 释放资源 + /// + public override void Dispose() + { + _logger.LogInformation("正在释放MQTT后台服务资源..."); + + _dataCenterService.OnLoadDataCompleted -= OnLoadDataCompleted; + _reloadSemaphore?.Dispose(); + + base.Dispose(); + + _logger.LogInformation("MQTT后台服务资源已释放"); + } } } \ No newline at end of file diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 77c4b1f..f3243ab 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -39,6 +39,7 @@ public partial class App : System.Windows.Application public IServiceProvider Services { get; } public new static App Current => (App)System.Windows.Application.Current; public IHost Host { get; } + private readonly ILogger _logger; public App() { @@ -53,6 +54,7 @@ public partial class App : System.Windows.Application }) .Build(); Services = Host.Services; + _logger = Host.Services.GetRequiredService>(); } protected override async void OnStartup(StartupEventArgs e) @@ -86,10 +88,60 @@ public partial class App : System.Windows.Application protected override async void OnExit(ExitEventArgs e) { - // 停止服务 - await Host.StopAsync(); - Host.Dispose(); - LogManager.Shutdown(); + _logger.LogInformation("应用程序正在关闭,开始清理资源..."); + + try + { + // 获取服务管理器 + var opcUaServiceManager = Host.Services.GetService(); + var s7ServiceManager = Host.Services.GetService(); + var mqttServiceManager = Host.Services.GetService(); + + // 优雅地关闭OPC UA服务 + if (opcUaServiceManager != null) + { + _logger.LogInformation("正在关闭OPC UA服务..."); + opcUaServiceManager.Dispose(); + _logger.LogInformation("OPC UA服务已关闭"); + } + + // 优雅地关闭S7服务 + if (s7ServiceManager != null) + { + _logger.LogInformation("正在关闭S7服务..."); + s7ServiceManager.Dispose(); + _logger.LogInformation("S7服务已关闭"); + } + + // 优雅地关闭MQTT服务 + if (mqttServiceManager != null) + { + _logger.LogInformation("正在关闭MQTT服务..."); + mqttServiceManager.Dispose(); + _logger.LogInformation("MQTT服务已关闭"); + } + + // 停止后台服务 + _logger.LogInformation("正在停止后台服务..."); + await Host.StopAsync(); + _logger.LogInformation("后台服务已停止"); + + // 释放Host资源 + _logger.LogInformation("正在释放Host资源..."); + Host.Dispose(); + _logger.LogInformation("Host资源已释放"); + + // 关闭NLog + _logger.LogInformation("正在关闭NLog..."); + LogManager.Shutdown(); + _logger.LogInformation("NLog已关闭"); + } + catch (Exception ex) + { + _logger.LogError(ex, "关闭应用程序时发生错误: {ErrorMessage}", ex.Message); + } + + _logger.LogInformation("应用程序已完全关闭"); base.OnExit(e); }