cleanup after DI graph rewrite

This commit is contained in:
2026-06-03 17:27:58 -05:00
parent b14189c294
commit 2ffd237ddd
19 changed files with 553 additions and 216 deletions
@@ -56,6 +56,7 @@ public sealed partial class MainForm : Form
private readonly SystemTray _systemTray;
private readonly UnitManager _unitManager;
private readonly UpdateVisitor _updateVisitor = new();
private readonly System.Threading.SynchronizationContext _uiSyncContext;
private int _delayCount;
private Form _plotForm;
@@ -164,6 +165,9 @@ public sealed partial class MainForm : Form
perSessionFileRotationMenuItem.Checked = _logger.FileRotationMethod == LoggerFileRotation.PerSession;
dailyFileRotationMenuItem.Checked = _logger.FileRotationMethod == LoggerFileRotation.Daily;
// Captured on the UI thread; HardwareAdded/HardwareRemoved can now fire on a background discovery thread
// (when a defer env var is set), so they re-dispatch here before touching the tree.
_uiSyncContext = System.Threading.SynchronizationContext.Current;
_computer.HardwareAdded += HardwareAdded;
_computer.HardwareRemoved += HardwareRemoved;
@@ -824,12 +828,24 @@ public sealed partial class MainForm : Form
private void HardwareAdded(IHardware hardware)
{
if (_uiSyncContext != null && System.Threading.SynchronizationContext.Current != _uiSyncContext)
{
_uiSyncContext.Post(_ => HardwareAdded(hardware), null);
return;
}
SubHardwareAdded(hardware, _root);
PlotSelectionChanged(this, null);
}
private void HardwareRemoved(IHardware hardware)
{
if (_uiSyncContext != null && System.Threading.SynchronizationContext.Current != _uiSyncContext)
{
_uiSyncContext.Post(_ => HardwareRemoved(hardware), null);
return;
}
List<HardwareNode> nodesToRemove = new();
foreach (Node node in _root.Nodes)
{
@@ -62,11 +62,14 @@ public class SensorGadget : Gadget
private StringFormat _alignRightStringFormat;
private Color _fontColor;
private Color _backgroundColor;
private readonly System.Threading.SynchronizationContext _uiSyncContext;
public SensorGadget(IComputer computer, PersistentSettings settings, UnitManager unitManager)
{
_unitManager = unitManager;
_settings = settings;
// Captured on the UI thread so the now-possibly-background HardwareAdded/HardwareRemoved re-dispatch here.
_uiSyncContext = System.Threading.SynchronizationContext.Current;
computer.HardwareAdded += HardwareAdded;
computer.HardwareRemoved += HardwareRemoved;
@@ -385,6 +388,12 @@ public class SensorGadget : Gadget
private void HardwareRemoved(IHardware hardware)
{
if (_uiSyncContext != null && System.Threading.SynchronizationContext.Current != _uiSyncContext)
{
_uiSyncContext.Post(_ => HardwareRemoved(hardware), null);
return;
}
hardware.SensorAdded -= SensorAdded;
hardware.SensorRemoved -= SensorRemoved;
@@ -397,6 +406,12 @@ public class SensorGadget : Gadget
private void HardwareAdded(IHardware hardware)
{
if (_uiSyncContext != null && System.Threading.SynchronizationContext.Current != _uiSyncContext)
{
_uiSyncContext.Post(_ => HardwareAdded(hardware), null);
return;
}
foreach (ISensor sensor in hardware.Sensors)
SensorAdded(sensor);
@@ -20,12 +20,15 @@ public class SystemTray : IDisposable
private readonly List<SensorNotifyIcon> _sensorList = new List<SensorNotifyIcon>();
private bool _mainIconEnabled;
private readonly NotifyIconAdv _mainIcon;
private readonly System.Threading.SynchronizationContext _uiSyncContext;
public SystemTray(IComputer computer, PersistentSettings settings, UnitManager unitManager)
{
_computer = computer;
_settings = settings;
_unitManager = unitManager;
// Captured on the UI thread so the now-possibly-background HardwareAdded/HardwareRemoved re-dispatch here.
_uiSyncContext = System.Threading.SynchronizationContext.Current;
computer.HardwareAdded += HardwareAdded;
computer.HardwareRemoved += HardwareRemoved;
@@ -56,6 +59,12 @@ public class SystemTray : IDisposable
private void HardwareRemoved(IHardware hardware)
{
if (_uiSyncContext != null && System.Threading.SynchronizationContext.Current != _uiSyncContext)
{
_uiSyncContext.Post(_ => HardwareRemoved(hardware), null);
return;
}
hardware.SensorAdded -= SensorAdded;
hardware.SensorRemoved -= SensorRemoved;
@@ -68,6 +77,12 @@ public class SystemTray : IDisposable
private void HardwareAdded(IHardware hardware)
{
if (_uiSyncContext != null && System.Threading.SynchronizationContext.Current != _uiSyncContext)
{
_uiSyncContext.Post(_ => HardwareAdded(hardware), null);
return;
}
foreach (ISensor sensor in hardware.Sensors)
SensorAdded(sensor);