diff --git a/StatusWindow/Data.cs b/StatusWindow/Data.cs index b120b31..c39733d 100644 --- a/StatusWindow/Data.cs +++ b/StatusWindow/Data.cs @@ -17,7 +17,7 @@ internal static class Data internal static ObservableCollection SensorEntries { get; set; } = []; - internal static async Task LoadComputer() + private static async Task LoadComputer() { try { @@ -30,8 +30,11 @@ internal static class Data } } - internal static void RefreshComputer() + internal static async Task RefreshComputer() { + if (_pipeClient is not { State: PipeState.Connected }) + await LoadComputer(); + _hardware = _pipeClient.InvokeAsync(service => service.GetHardware()).Result; } diff --git a/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml.cs b/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml.cs index 2f2163c..c731cb3 100644 --- a/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml.cs +++ b/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml.cs @@ -67,13 +67,13 @@ public partial class HardwareSettingsPanel EditSelectedSensor(); } - private void AddSensor() + private async void AddSensor() { var sensorEntry = new SensorEntry(); var sensorWindow = new SensorWindow(); - var result = sensorWindow.Display(sensorEntry, Window.GetWindow(this)); + var result = await sensorWindow.Display(sensorEntry, Window.GetWindow(this)); if (!result.HasValue || !result.Value) return; @@ -83,7 +83,7 @@ public partial class HardwareSettingsPanel SetSensorButtonStates(); } - private void EditSelectedSensor() + private async void EditSelectedSensor() { if (SensorDataGrid.SelectedItem == null) return; @@ -92,7 +92,7 @@ public partial class HardwareSettingsPanel var sensorWindow = new SensorWindow(); - sensorWindow.Display(sensorEntry, Window.GetWindow(this)); + await sensorWindow.Display(sensorEntry, Window.GetWindow(this)); } private void DeleteSelectedSensors() diff --git a/StatusWindow/SettingsWindow/SensorWindow.xaml.cs b/StatusWindow/SettingsWindow/SensorWindow.xaml.cs index 2c1bc0b..a85142a 100644 --- a/StatusWindow/SettingsWindow/SensorWindow.xaml.cs +++ b/StatusWindow/SettingsWindow/SensorWindow.xaml.cs @@ -2,6 +2,7 @@ using ChrisKaczor.Wpf.Validation; using HardwareMonitorStatusWindow.Service; using System.Linq; +using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Threading; @@ -17,11 +18,11 @@ public partial class SensorWindow InitializeComponent(); } - public bool? Display(SensorEntry sensorEntry, Window? owner) + public async Task Display(SensorEntry sensorEntry, Window? owner) { DataContext = sensorEntry; - Data.RefreshComputer(); + await Data.RefreshComputer(); HardwareTypeComboBox.ItemsSource = Data.ComputerHardware.Where(h => h.Sensors.Any()).DistinctBy(h => h.Type).Select(s => new HardwareTypeItem(s.Type)).OrderBy(s => s.Name); diff --git a/StatusWindow/WindowSource.cs b/StatusWindow/WindowSource.cs index f38e306..ce61cce 100644 --- a/StatusWindow/WindowSource.cs +++ b/StatusWindow/WindowSource.cs @@ -139,7 +139,7 @@ internal class WindowSource : IWindowSource, IDisposable return true; } - private async Task Start(bool hasUpdate) + private void Start(bool hasUpdate) { Log.Information("Start: hasUpdate={hasUpdate}", hasUpdate); @@ -148,7 +148,7 @@ internal class WindowSource : IWindowSource, IDisposable Log.Information("Load"); - await Load(); + Load(); Log.Information("Starting timer"); @@ -157,10 +157,8 @@ internal class WindowSource : IWindowSource, IDisposable _timer.Enabled = true; } - private static async Task Load() + private static void Load() { - await Data.LoadComputer(); - Data.Load(); } @@ -227,19 +225,24 @@ internal class WindowSource : IWindowSource, IDisposable if (existingTask == null) { _dispatcher.Invoke(() => _floatingStatusWindow.SetText(Resources.ServiceNotInstalled)); + _timer.Start(); return; } if (existingTask.State != TaskState.Running) { _dispatcher.Invoke(() => _floatingStatusWindow.SetText(Resources.ServiceNotStarted)); + + existingTask.Run(); + _timer.Start(); + return; } } var text = new StringBuilder(); - Data.RefreshComputer(); + Data.RefreshComputer().Wait(); foreach (var sensorEntry in Data.SensorEntries) {