11using System ;
2- using System . Collections . Concurrent ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Windows . Forms ;
45using OpenHardwareMonitor . Hardware ;
56using OpenHardwareMonitor . Utilities ;
@@ -9,7 +10,7 @@ namespace OpenHardwareMonitor.UI;
910public class SystemTray : IDisposable
1011{
1112 private readonly PersistentSettings _settings ;
12- private readonly ConcurrentDictionary < string , SensorNotifyIcon > _sensorList = new ( ) ;
13+ private readonly List < SensorNotifyIcon > _sensorList = new ( ) ;
1314 private bool _mainIconEnabled ;
1415 private readonly NotifyIconAdv _mainIcon ;
1516
@@ -82,30 +83,32 @@ private void SensorRemoved(ISensor sensor)
8283
8384 public void Dispose ( )
8485 {
85- foreach ( SensorNotifyIcon icon in _sensorList . Values )
86+ lock ( _sensorList )
87+ foreach ( SensorNotifyIcon icon in _sensorList )
8688 icon . Dispose ( ) ;
8789 _mainIcon . Dispose ( ) ;
8890 }
8991
9092 public void Redraw ( )
9193 {
92- foreach ( SensorNotifyIcon icon in _sensorList . Values )
94+ lock ( _sensorList )
95+ foreach ( SensorNotifyIcon icon in _sensorList )
9396 icon . Update ( ) ;
9497 }
9598
96- public bool Contains ( ISensor sensor ) => _sensorList . ContainsKey ( sensor . Identifier . ToString ( ) ) ;
99+ public bool Contains ( ISensor sensor )
100+ {
101+ lock ( _sensorList )
102+ return _sensorList . Any ( x => x . Sensor . Identifier . ToString ( ) == sensor . Identifier . ToString ( ) ) ;
103+ }
97104
98- public bool Add ( ISensor sensor , bool replaceExisting = true )
105+ public bool Add ( ISensor sensor )
99106 {
100107 if ( Contains ( sensor ) )
101- {
102- if ( replaceExisting )
103- Remove ( sensor ) ;
104- else
105- return false ;
106- }
108+ return false ;
107109
108- _sensorList [ sensor . Identifier . ToString ( ) ] = new SensorNotifyIcon ( this , sensor , _settings ) ;
110+ lock ( _sensorList )
111+ _sensorList . Add ( new SensorNotifyIcon ( this , sensor , _settings ) ) ;
109112 UpdateMainIconVisibility ( ) ;
110113 _settings . SetValue ( new Identifier ( sensor . Identifier , "tray" ) . ToString ( ) , true ) ;
111114 return true ;
@@ -123,10 +126,17 @@ private void Remove(ISensor sensor, bool deleteConfig)
123126 _settings . Remove ( new Identifier ( sensor . Identifier , "tray" ) . ToString ( ) ) ;
124127 _settings . Remove ( new Identifier ( sensor . Identifier , "traycolor" ) . ToString ( ) ) ;
125128 }
126- if ( _sensorList . TryRemove ( sensor . Identifier . ToString ( ) , out SensorNotifyIcon instance ) )
129+
130+ lock ( _sensorList )
127131 {
128- UpdateMainIconVisibility ( ) ;
129- instance . Dispose ( ) ;
132+ var toRemove = _sensorList . Where ( s => s . Sensor . Identifier . ToString ( ) == sensor . Identifier . ToString ( ) )
133+ . ToArray ( ) ;
134+ foreach ( var instance in toRemove )
135+ {
136+ _sensorList . Remove ( instance ) ;
137+ UpdateMainIconVisibility ( ) ;
138+ instance . Dispose ( ) ;
139+ }
130140 }
131141 }
132142
@@ -147,6 +157,7 @@ public void SendExitCommand()
147157 private void UpdateMainIconVisibility ( )
148158 {
149159 if ( _mainIconEnabled )
160+ lock ( _sensorList )
150161 _mainIcon . Visible = _sensorList . Count == 0 ;
151162 else
152163 _mainIcon . Visible = false ;
0 commit comments