From 9454fab553d50b0d9fcbecca909d22893fb8b8cb Mon Sep 17 00:00:00 2001 From: Phyxion <7643972+Phyxion@users.noreply.github.com> Date: Tue, 21 Jul 2020 16:10:44 +0200 Subject: [PATCH] Fixes #266 --- LibreHardwareMonitorLib/Hardware/Computer.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/LibreHardwareMonitorLib/Hardware/Computer.cs b/LibreHardwareMonitorLib/Hardware/Computer.cs index 291ae89..7c7eff5 100644 --- a/LibreHardwareMonitorLib/Hardware/Computer.cs +++ b/LibreHardwareMonitorLib/Hardware/Computer.cs @@ -307,10 +307,13 @@ namespace LibreHardwareMonitor.Hardware { lock (_lock) { - foreach (IGroup group in _groups) + // Use a for-loop instead of foreach to avoid a collection modified exception after sleep, even though everything is under a lock. + for (int i = 0; i < _groups.Count; i++) { - foreach (IHardware hardware in group.Hardware) - hardware.Accept(visitor); + IGroup group = _groups[i]; + + for (int j = 0; j < group.Hardware.Count; j++) + group.Hardware[j].Accept(visitor); } } } @@ -356,18 +359,18 @@ namespace LibreHardwareMonitor.Hardware private void RemoveType() where T : IGroup { - List list = new List(); + List list = new List(); lock (_lock) { foreach (IGroup group in _groups) { - if (group is T) - list.Add(group); + if (group is T t) + list.Add(t); } } - foreach (IGroup group in list) + foreach (T group in list) Remove(group); }