From fc74039044b3ff0bb1eab4f2a3b52009b374c2f3 Mon Sep 17 00:00:00 2001
From: Paul <25939765+Cereal-Killa@users.noreply.github.com>
Date: Sat, 22 Jan 2022 07:21:33 -0300
Subject: [PATCH] Fluid UI by updating sensors in a background thread. (#626)
* On computers with lots of devices/sensors, the timer update routine will cause a performance hit on the main window redraw when moving it around. This commit address the issue by moving the timer routine to a backgroundworker object that does the work in an async manner (thread).
* White spaces.
* White spaces.
* Keep the ui redraw in the main thread to prevent breaking the tray context menu for the instance.
---
LibreHardwareMonitor/UI/MainForm.Designer.cs | 2 ++
LibreHardwareMonitor/UI/MainForm.cs | 23 ++++++++++++++------
LibreHardwareMonitor/UI/MainForm.resx | 3 +++
3 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/LibreHardwareMonitor/UI/MainForm.Designer.cs b/LibreHardwareMonitor/UI/MainForm.Designer.cs
index 920944e..961f608 100644
--- a/LibreHardwareMonitor/UI/MainForm.Designer.cs
+++ b/LibreHardwareMonitor/UI/MainForm.Designer.cs
@@ -126,6 +126,7 @@ namespace LibreHardwareMonitor.UI
this.splitContainer = new LibreHardwareMonitor.UI.SplitContainerAdv();
this.treeView = new Aga.Controls.Tree.TreeViewAdv();
this.psuMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+ this.backgroundUpdater = new System.ComponentModel.BackgroundWorker();
this.mainMenu.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit();
this.splitContainer.Panel1.SuspendLayout();
@@ -969,6 +970,7 @@ namespace LibreHardwareMonitor.UI
private ToolStripRadioButtonMenuItem timeWindow24hMenuItem;
private System.Windows.Forms.ToolStripMenuItem authWebServerMenuItem;
private System.Windows.Forms.ToolStripMenuItem psuMenuItem;
+ private System.ComponentModel.BackgroundWorker backgroundUpdater;
}
}
diff --git a/LibreHardwareMonitor/UI/MainForm.cs b/LibreHardwareMonitor/UI/MainForm.cs
index 4fabdf8..16c3ba0 100644
--- a/LibreHardwareMonitor/UI/MainForm.cs
+++ b/LibreHardwareMonitor/UI/MainForm.cs
@@ -163,6 +163,7 @@ namespace LibreHardwareMonitor.UI
_computer.HardwareRemoved += HardwareRemoved;
_computer.Open();
+ backgroundUpdater.DoWork += BackgroundUpdater_DoWork;
timer.Enabled = true;
UserOption showHiddenSensors = new UserOption("hiddenMenuItem", false, hiddenMenuItem, _settings);
@@ -377,6 +378,17 @@ namespace LibreHardwareMonitor.UI
Microsoft.Win32.SystemEvents.PowerModeChanged += PowerModeChanged;
}
+ private void BackgroundUpdater_DoWork(object sender, DoWorkEventArgs e)
+ {
+ _computer.Accept(_updateVisitor);
+
+ if (_logSensors != null && _logSensors.Value && _delayCount >= 4)
+ _logger.Log();
+
+ if (_delayCount < 4)
+ _delayCount++;
+ }
+
private void PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs eventArgs)
{
if (eventArgs.Mode == Microsoft.Win32.PowerModes.Resume)
@@ -636,19 +648,14 @@ namespace LibreHardwareMonitor.UI
private void Timer_Tick(object sender, EventArgs e)
{
- _computer.Accept(_updateVisitor);
-
treeView.Invalidate();
_plotPanel.InvalidatePlot();
_systemTray.Redraw();
_gadget?.Redraw();
_wmiProvider?.Update();
- if (_logSensors != null && _logSensors.Value && _delayCount >= 4)
- _logger.Log();
-
- if (_delayCount < 4)
- _delayCount++;
+ if (!backgroundUpdater.IsBusy)
+ backgroundUpdater.RunWorkerAsync();
RestoreCollapsedNodeState(treeView);
}
@@ -742,6 +749,8 @@ namespace LibreHardwareMonitor.UI
if (_runWebServer.Value)
Server.Quit();
_systemTray.Dispose();
+ timer.Dispose();
+ backgroundUpdater.Dispose();
Application.Exit();
}
diff --git a/LibreHardwareMonitor/UI/MainForm.resx b/LibreHardwareMonitor/UI/MainForm.resx
index ee0fc07..17a6d78 100644
--- a/LibreHardwareMonitor/UI/MainForm.resx
+++ b/LibreHardwareMonitor/UI/MainForm.resx
@@ -126,6 +126,9 @@
293, 17
+
+ 483, 17
+