From 8b7658a4a33241617456b87edda4ba5d3ceed639 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Mon, 7 Jul 2025 21:24:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=BC=80=E5=85=B3=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=94=B9=E4=B8=BA=E5=AE=9E=E6=97=B6=E5=93=8D?= =?UTF-8?q?=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 25 +++++++++++++++++-------- Services/MqttBackgroundService.cs | 18 +++++++----------- Services/S7BackgroundService.cs | 20 +++++++------------- ViewModels/SettingViewModel.cs | 23 ++++++++++++++++++++++- Views/SettingView.xaml.cs | 3 ++- 5 files changed, 55 insertions(+), 34 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index 1c09666..2c88a05 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -66,10 +66,25 @@ public partial class App : Application MainWindow = Host.Services.GetRequiredService(); MainWindow.Show(); + + // 根据配置启动服务 + var connectionSettings = PMSWPF.Config.ConnectionSettings.Load(); + if (connectionSettings.EnableS7Service) + { + Host.Services.GetRequiredService().StartService(); + } + if (connectionSettings.EnableMqttService) + { + Host.Services.GetRequiredService().StartService(); + } } protected override async void OnExit(ExitEventArgs e) { + // 停止服务 + Host.Services.GetRequiredService().StopService(); + Host.Services.GetRequiredService().StopService(); + await Host.StopAsync(); Host.Dispose(); LogManager.Shutdown(); @@ -82,14 +97,8 @@ public partial class App : Application services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - if (ConnectionSettings.Load().EnableS7Service) - { - services.AddHostedService(); // Register as HostedService - } - if (ConnectionSettings.Load().EnableMqttService) - { - services.AddHostedService(); - } + services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/Services/MqttBackgroundService.cs b/Services/MqttBackgroundService.cs index 13945be..a230a90 100644 --- a/Services/MqttBackgroundService.cs +++ b/Services/MqttBackgroundService.cs @@ -17,7 +17,7 @@ namespace PMSWPF.Services /// /// MQTT后台服务,继承自BackgroundService,用于在后台管理MQTT连接和数据发布。 /// - public class MqttBackgroundService : BackgroundService + public class MqttBackgroundService { // 数据服务实例,用于访问和操作应用程序数据,如MQTT配置和变量数据。 private readonly DataServices _dataServices; @@ -43,11 +43,9 @@ namespace PMSWPF.Services } /// - /// 后台服务的执行方法,当服务启动时调用。 + /// 启动MQTT后台服务。 /// - /// 用于取消操作的CancellationToken。 - /// 表示异步操作的任务。 - protected override async Task ExecuteAsync(CancellationToken stoppingToken) + public async void StartService() { NlogHelper.Info("MqttBackgroundService started."); // 记录服务启动信息 @@ -63,15 +61,13 @@ namespace PMSWPF.Services _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(5)); // 每5秒轮询一次 // 使服务保持运行,直到收到停止请求。 - await Task.Delay(Timeout.Infinite, stoppingToken); + // await Task.Delay(Timeout.Infinite, stoppingToken); } /// - /// 后台服务的停止方法,当服务停止时调用。 + /// 停止MQTT后台服务。 /// - /// 用于取消操作的CancellationToken。 - /// 表示异步操作的任务。 - public override async Task StopAsync(CancellationToken stoppingToken) + public async void StopService() { NlogHelper.Info("MqttBackgroundService stopping."); // 记录服务停止信息 @@ -95,7 +91,7 @@ namespace PMSWPF.Services _mqttConfigurations.Clear(); _mqttVariableData.Clear(); - await base.StopAsync(stoppingToken); + } /// diff --git a/Services/S7BackgroundService.cs b/Services/S7BackgroundService.cs index 108a39d..18da0f4 100644 --- a/Services/S7BackgroundService.cs +++ b/Services/S7BackgroundService.cs @@ -16,7 +16,7 @@ namespace PMSWPF.Services /// /// S7后台服务,继承自BackgroundService,用于在后台周期性地轮询S7 PLC设备数据。 /// - public class S7BackgroundService : BackgroundService + public class S7BackgroundService { // 数据服务实例,用于访问和操作应用程序数据,如设备配置。 private readonly DataServices _dataServices; @@ -91,15 +91,13 @@ namespace PMSWPF.Services } /// - /// 后台服务的执行方法,当服务启动时调用。 + /// 启动S7后台服务。 /// - /// 用于取消操作的CancellationToken。 - /// 表示异步操作的任务。 - protected override Task ExecuteAsync(CancellationToken stoppingToken) + public void StartService() { NlogHelper.Info("S7后台服务正在启动。"); // 创建一个CancellationTokenSource,用于控制轮询线程的取消。 - _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken); + _cancellationTokenSource = new CancellationTokenSource(); // 创建并启动轮询线程。 _pollingThread = new Thread(() => PollingLoop(_cancellationTokenSource.Token)) @@ -107,8 +105,6 @@ namespace PMSWPF.Services IsBackground = true // 设置为后台线程,随主程序退出而退出 }; _pollingThread.Start(); - - return Task.CompletedTask; } /// @@ -307,11 +303,9 @@ namespace PMSWPF.Services } /// - /// 后台服务的停止方法,当服务停止时调用。 + /// 停止S7后台服务。 /// - /// 用于取消操作的CancellationToken。 - /// 表示异步操作的任务。 - public override async Task StopAsync(CancellationToken stoppingToken) + public void StopService() { NlogHelper.Info("S7 Background Service is stopping."); @@ -332,7 +326,7 @@ namespace PMSWPF.Services _s7PlcClients.Clear(); // 清空PLC客户端字典。 - await base.StopAsync(stoppingToken); + } } } \ No newline at end of file diff --git a/ViewModels/SettingViewModel.cs b/ViewModels/SettingViewModel.cs index 280f670..43de70c 100644 --- a/ViewModels/SettingViewModel.cs +++ b/ViewModels/SettingViewModel.cs @@ -7,17 +7,22 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using PMSWPF.Services; namespace PMSWPF.ViewModels; public partial class SettingViewModel : ViewModelBase { private ConnectionSettings _connectionSettings; + private readonly S7BackgroundService _s7BackgroundService; + private readonly MqttBackgroundService _mqttBackgroundService; - public SettingViewModel() + public SettingViewModel(S7BackgroundService s7BackgroundService, MqttBackgroundService mqttBackgroundService) { _connectionSettings = ConnectionSettings.Load(); AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList(); + _s7BackgroundService = s7BackgroundService; + _mqttBackgroundService = mqttBackgroundService; } public List AvailableDbTypes { get; set; } @@ -116,6 +121,14 @@ public partial class SettingViewModel : ViewModelBase _connectionSettings.EnableS7Service = value; OnPropertyChanged(); _connectionSettings.Save(); + if (value) + { + _s7BackgroundService.StartService(); + } + else + { + _s7BackgroundService.StopService(); + } } } } @@ -130,6 +143,14 @@ public partial class SettingViewModel : ViewModelBase _connectionSettings.EnableMqttService = value; OnPropertyChanged(); _connectionSettings.Save(); + if (value) + { + _mqttBackgroundService.StartService(); + } + else + { + _mqttBackgroundService.StopService(); + } } } } diff --git a/Views/SettingView.xaml.cs b/Views/SettingView.xaml.cs index 99a9b66..3f436e5 100644 --- a/Views/SettingView.xaml.cs +++ b/Views/SettingView.xaml.cs @@ -1,5 +1,6 @@ using PMSWPF.ViewModels; using System.Windows.Controls; +using Microsoft.Extensions.DependencyInjection; namespace PMSWPF.Views; @@ -8,6 +9,6 @@ public partial class SettingView : UserControl public SettingView() { InitializeComponent(); - DataContext = new SettingViewModel(); + DataContext = App.Current.Services.GetRequiredService(); } } \ No newline at end of file