11using System ;
2- using System . Collections . Generic ;
3- using System . Linq ;
2+ using System . Collections . Concurrent ;
43using System . Windows . Forms ;
54using OpenHardwareMonitor . Hardware ;
65using OpenHardwareMonitor . Utilities ;
@@ -10,7 +9,7 @@ namespace OpenHardwareMonitor.UI;
109public class SystemTray : IDisposable
1110{
1211 private readonly PersistentSettings _settings ;
13- private readonly List < SensorNotifyIcon > _sensorList = new List < SensorNotifyIcon > ( ) ;
12+ private readonly ConcurrentDictionary < string , SensorNotifyIcon > _sensorList = new ( ) ;
1413 private bool _mainIconEnabled ;
1514 private readonly NotifyIconAdv _mainIcon ;
1615
@@ -78,37 +77,35 @@ private void SensorAdded(ISensor sensor)
7877
7978 private void SensorRemoved ( ISensor sensor )
8079 {
81- if ( Contains ( sensor ) )
82- Remove ( sensor , false ) ;
80+ Remove ( sensor , false ) ;
8381 }
8482
8583 public void Dispose ( )
8684 {
87- foreach ( SensorNotifyIcon icon in _sensorList )
85+ foreach ( SensorNotifyIcon icon in _sensorList . Values )
8886 icon . Dispose ( ) ;
8987 _mainIcon . Dispose ( ) ;
9088 }
9189
9290 public void Redraw ( )
9391 {
94- SensorNotifyIcon [ ] sensorsToRedraw ;
95- lock ( _sensorList )
96- {
97- sensorsToRedraw = _sensorList . ToArray ( ) ;
98- }
99-
100- foreach ( SensorNotifyIcon icon in sensorsToRedraw )
92+ foreach ( SensorNotifyIcon icon in _sensorList . Values )
10193 icon . Update ( ) ;
10294 }
10395
104- public bool Contains ( ISensor sensor ) => _sensorList . Any ( icon => icon . Sensor == sensor ) ;
96+ public bool Contains ( ISensor sensor ) => _sensorList . ContainsKey ( sensor . Identifier . ToString ( ) ) ;
10597
106- public bool Add ( ISensor sensor )
98+ public bool Add ( ISensor sensor , bool replaceExisting = true )
10799 {
108100 if ( Contains ( sensor ) )
109- return false ;
101+ {
102+ if ( replaceExisting )
103+ Remove ( sensor ) ;
104+ else
105+ return false ;
106+ }
110107
111- _sensorList . Add ( new SensorNotifyIcon ( this , sensor , _settings ) ) ;
108+ _sensorList [ sensor . Identifier . ToString ( ) ] = new SensorNotifyIcon ( this , sensor , _settings ) ;
112109 UpdateMainIconVisibility ( ) ;
113110 _settings . SetValue ( new Identifier ( sensor . Identifier , "tray" ) . ToString ( ) , true ) ;
114111 return true ;
@@ -126,13 +123,8 @@ private void Remove(ISensor sensor, bool deleteConfig)
126123 _settings . Remove ( new Identifier ( sensor . Identifier , "tray" ) . ToString ( ) ) ;
127124 _settings . Remove ( new Identifier ( sensor . Identifier , "traycolor" ) . ToString ( ) ) ;
128125 }
129- SensorNotifyIcon instance ;
130- lock ( _sensorList )
131- instance = _sensorList . FirstOrDefault ( icon => icon . Sensor == sensor ) ;
132- if ( instance != null )
126+ if ( _sensorList . TryRemove ( sensor . Identifier . ToString ( ) , out SensorNotifyIcon instance ) )
133127 {
134- lock ( _sensorList )
135- _sensorList . Remove ( instance ) ;
136128 UpdateMainIconVisibility ( ) ;
137129 instance . Dispose ( ) ;
138130 }
0 commit comments