diff --git a/HSMonitor/Services/HardwareMonitorService.cs b/HSMonitor/Services/HardwareMonitorService.cs index e642e84..acec55f 100644 --- a/HSMonitor/Services/HardwareMonitorService.cs +++ b/HSMonitor/Services/HardwareMonitorService.cs @@ -1,10 +1,14 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Threading; using HSMonitor.Models; using HSMonitor.Utils.Logger; using HSMonitor.Utils.Serial; using LibreHardwareMonitor.Hardware; +using MaterialDesignThemes.Wpf; namespace HSMonitor.Services; @@ -19,6 +23,8 @@ public class HardwareMonitorService public event EventHandler? HardwareInformationUpdated; + private DispatcherTimer _updateHardwareMonitorTimer = new(); + private static readonly Computer Computer = new() { IsCpuEnabled = true, @@ -34,10 +40,30 @@ public HardwareMonitorService(SettingsService settingsService, ILogger + { + _updateHardwareMonitorTimer.Start(); + }); + } + private void SettingsServiceOnSettingsSaved(object? sender, EventArgs e) { if (sender is not SettingsService service) return; _settingsService.Settings = service.Settings; + _updateHardwareMonitorTimer.Interval = TimeSpan.FromMilliseconds( + _settingsService.Settings.SendInterval == 0 + ? 500 + :_settingsService.Settings.SendInterval); } public Message GetHwInfoMessage() @@ -58,7 +84,7 @@ public static IEnumerable GetProcessors() => Computer.Hardware.Where( public static IEnumerable GetGraphicCards() => Computer.Hardware.Where(h => h.HardwareType is HardwareType.GpuAmd or HardwareType.GpuIntel or HardwareType.GpuNvidia); - public void HardwareInformationUpdate() + public async void HardwareInformationUpdate(object? sender, EventArgs eventArgs) { try { diff --git a/HSMonitor/ViewModels/MainWindowViewModel.cs b/HSMonitor/ViewModels/MainWindowViewModel.cs index dc36b50..d537fd1 100644 --- a/HSMonitor/ViewModels/MainWindowViewModel.cs +++ b/HSMonitor/ViewModels/MainWindowViewModel.cs @@ -27,8 +27,6 @@ public class MainWindowViewModel : Screen private readonly HardwareMonitorService _hardwareMonitorService; private readonly UpdateService _updateService; private readonly ILogger _logger; - - private DispatcherTimer _updateHardwareMonitorTimer = null!; public DashboardViewModel Dashboard { get; } private bool _isConnectionErrorWindowOpened; @@ -175,21 +173,6 @@ public async void OnViewFullyLoaded() } else { - _updateHardwareMonitorTimer = new DispatcherTimer( - priority: DispatcherPriority.Background, - interval: TimeSpan.FromMilliseconds(_settingsService.Settings.SendInterval == 0 - ? 500 - : _settingsService.Settings.SendInterval), - callback: (_, _) => { _hardwareMonitorService.HardwareInformationUpdate(); }, - dispatcher: Dispatcher.FromThread(Thread.CurrentThread) ?? throw new InvalidOperationException() - ); - - _settingsService.SettingsSaved += (_, _) => - { - _updateHardwareMonitorTimer.Interval = TimeSpan.FromMilliseconds( - _settingsService.Settings.SendInterval == 0 ? 500 : _settingsService.Settings.SendInterval); - }; - _serialMonitorService.OpenPortAttemptSuccessful += SerialMonitorServiceOnOpenPortAttemptSuccessful; try @@ -250,7 +233,7 @@ public async void OnViewFullyLoaded() await ShowAdminPrivilegesRequirement(); } - _updateHardwareMonitorTimer.Start(); + await _hardwareMonitorService.Start(); } }