From 738752b8999913fcd02956124d50eb410db4178d Mon Sep 17 00:00:00 2001 From: Phyxion <7643972+Phyxion@users.noreply.github.com> Date: Mon, 25 May 2020 12:09:05 +0200 Subject: [PATCH] Add PowerModeChanged event handler to reset the hardware tree when resuming from sleep Based on the work of @moellerm --- LibreHardwareMonitor/UI/MainForm.cs | 15 +++++++++- LibreHardwareMonitorLib/Hardware/Computer.cs | 30 ++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/LibreHardwareMonitor/UI/MainForm.cs b/LibreHardwareMonitor/UI/MainForm.cs index b6eb0a4..745c00c 100644 --- a/LibreHardwareMonitor/UI/MainForm.cs +++ b/LibreHardwareMonitor/UI/MainForm.cs @@ -123,7 +123,7 @@ namespace LibreHardwareMonitor.UI _systemTray.ExitCommand += ExitClick; if (Software.OperatingSystem.IsUnix) - { + { // Unix treeView.RowHeight = Math.Max(treeView.RowHeight, 18); splitContainer.BorderStyle = BorderStyle.None; @@ -355,7 +355,9 @@ namespace LibreHardwareMonitor.UI } } else + { Show(); + } // Create a handle, otherwise calling Close() does not fire FormClosed @@ -367,6 +369,17 @@ namespace LibreHardwareMonitor.UI if (_runWebServer.Value) Server.Quit(); }; + + Microsoft.Win32.SystemEvents.PowerModeChanged += PowerModeChanged; + } + + private void PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs eventArgs) + { + + if (eventArgs.Mode == Microsoft.Win32.PowerModes.Resume) + { + _computer.Reset(); + } } private void InitializeSplitter() diff --git a/LibreHardwareMonitorLib/Hardware/Computer.cs b/LibreHardwareMonitorLib/Hardware/Computer.cs index b84f57c..34c5090 100644 --- a/LibreHardwareMonitorLib/Hardware/Computer.cs +++ b/LibreHardwareMonitorLib/Hardware/Computer.cs @@ -25,8 +25,8 @@ namespace LibreHardwareMonitor.Hardware { private readonly List _groups = new List(); private readonly ISettings _settings; - private bool _cpuEnabled; private bool _controllerEnabled; + private bool _cpuEnabled; private bool _gpuEnabled; private bool _memoryEnabled; private bool _motherboardEnabled; @@ -359,6 +359,13 @@ namespace LibreHardwareMonitor.Hardware Ring0.Open(); OpCode.Open(); + AddGroups(); + + _open = true; + } + + private void AddGroups() + { if (_motherboardEnabled) Add(new MotherboardGroup(_smbios, _settings)); @@ -387,8 +394,6 @@ namespace LibreHardwareMonitor.Hardware if (_networkEnabled) Add(new NetworkGroup(_settings)); - - _open = true; } private static void NewSection(TextWriter writer) @@ -481,6 +486,25 @@ namespace LibreHardwareMonitor.Hardware _open = false; } + public void Reset() + { + if (!_open) + return; + + + RemoveGroups(); + AddGroups(); + } + + private void RemoveGroups() + { + while (_groups.Count > 0) + { + IGroup group = _groups[_groups.Count - 1]; + Remove(group); + } + } + private class Settings : ISettings { public bool Contains(string name)