Skip to content

Commit

Permalink
The PC information update timer has been moved to another service
Browse files Browse the repository at this point in the history
  • Loading branch information
TTLC198 committed Nov 11, 2023
1 parent 8f89382 commit e13b54c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
28 changes: 27 additions & 1 deletion HSMonitor/Services/HardwareMonitorService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,6 +23,8 @@ public class HardwareMonitorService

public event EventHandler? HardwareInformationUpdated;

private DispatcherTimer _updateHardwareMonitorTimer = new();

private static readonly Computer Computer = new()
{
IsCpuEnabled = true,
Expand All @@ -34,10 +40,30 @@ public HardwareMonitorService(SettingsService settingsService, ILogger<HardwareM
_settingsService.SettingsSaved += SettingsServiceOnSettingsSaved;
}

public async Task Start()
{
_updateHardwareMonitorTimer = new DispatcherTimer(
priority: DispatcherPriority.Background,
interval: TimeSpan.FromMilliseconds(_settingsService.Settings.SendInterval == 0
? 500
: _settingsService.Settings.SendInterval),
callback: HardwareInformationUpdate,
dispatcher: Dispatcher.FromThread(Thread.CurrentThread) ?? throw new InvalidOperationException("Current thread is null")
);
await Task.Run(() =>
{
_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()
Expand All @@ -58,7 +84,7 @@ public static IEnumerable<IHardware> GetProcessors() => Computer.Hardware.Where(
public static IEnumerable<IHardware> 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
{
Expand Down
19 changes: 1 addition & 18 deletions HSMonitor/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class MainWindowViewModel : Screen
private readonly HardwareMonitorService _hardwareMonitorService;
private readonly UpdateService _updateService;
private readonly ILogger<MainWindowViewModel> _logger;

private DispatcherTimer _updateHardwareMonitorTimer = null!;
public DashboardViewModel Dashboard { get; }

private bool _isConnectionErrorWindowOpened;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -250,7 +233,7 @@ public async void OnViewFullyLoaded()
await ShowAdminPrivilegesRequirement();
}

_updateHardwareMonitorTimer.Start();
await _hardwareMonitorService.Start();
}
}

Expand Down

0 comments on commit e13b54c

Please sign in to comment.