@@ -12,6 +12,8 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
1212{
1313 #region Properties & Fields
1414
15+ private bool _isDisposed = false ;
16+
1517 private readonly double _defaultUpdateRateHardLimit ;
1618
1719 /// <inheritdoc />
@@ -60,13 +62,17 @@ protected AbstractRGBDeviceProvider(double defaultUpdateRateHardLimit = 0)
6062 this . _defaultUpdateRateHardLimit = defaultUpdateRateHardLimit ;
6163 }
6264
65+ ~ AbstractRGBDeviceProvider ( ) => Dispose ( false ) ;
66+
6367 #endregion
6468
6569 #region Methods
6670
6771 /// <inheritdoc />
6872 public bool Initialize ( RGBDeviceType loadFilter = RGBDeviceType . All , bool throwExceptions = false )
6973 {
74+ if ( _isDisposed ) throw new ObjectDisposedException ( GetType ( ) . FullName ) ;
75+
7076 ThrowsExceptions = throwExceptions ;
7177
7278 try
@@ -108,6 +114,8 @@ public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool throwE
108114 /// <returns>The filtered collection of loaded devices.</returns>
109115 protected virtual IEnumerable < IRGBDevice > GetLoadedDevices ( RGBDeviceType loadFilter )
110116 {
117+ if ( _isDisposed ) throw new ObjectDisposedException ( GetType ( ) . FullName ) ;
118+
111119 List < IRGBDevice > devices = new ( ) ;
112120 foreach ( IRGBDevice device in LoadDevices ( ) )
113121 {
@@ -152,6 +160,8 @@ protected virtual IEnumerable<IRGBDevice> GetLoadedDevices(RGBDeviceType loadFil
152160 /// <returns>The update trigger mapped to the specified id.</returns>
153161 protected virtual IDeviceUpdateTrigger GetUpdateTrigger ( int id = - 1 , double ? updateRateHardLimit = null )
154162 {
163+ if ( _isDisposed ) throw new ObjectDisposedException ( GetType ( ) . FullName ) ;
164+
155165 if ( ! UpdateTriggerMapping . TryGetValue ( id , out IDeviceUpdateTrigger ? updaeTrigger ) )
156166 UpdateTriggerMapping [ id ] = ( updaeTrigger = CreateUpdateTrigger ( id , updateRateHardLimit ?? _defaultUpdateRateHardLimit ) ) ;
157167
@@ -171,6 +181,8 @@ protected virtual IDeviceUpdateTrigger GetUpdateTrigger(int id = -1, double? upd
171181 /// </summary>
172182 protected virtual void Reset ( )
173183 {
184+ if ( _isDisposed ) throw new ObjectDisposedException ( GetType ( ) . FullName ) ;
185+
174186 foreach ( IDeviceUpdateTrigger updateTrigger in UpdateTriggerMapping . Values )
175187 updateTrigger . Dispose ( ) ;
176188
@@ -192,6 +204,8 @@ protected virtual void Reset()
192204 /// <returns><c>true</c> if the device was added successfully; otherwise <c>false</c>.</returns>
193205 protected virtual bool AddDevice ( IRGBDevice device )
194206 {
207+ if ( _isDisposed ) throw new ObjectDisposedException ( GetType ( ) . FullName ) ;
208+
195209 if ( InternalDevices . Contains ( device ) ) return false ;
196210
197211 InternalDevices . Add ( device ) ;
@@ -207,6 +221,8 @@ protected virtual bool AddDevice(IRGBDevice device)
207221 /// <returns><c>true</c> if the device was removed successfully; otherwise <c>false</c>.</returns>
208222 protected virtual bool RemoveDevice ( IRGBDevice device )
209223 {
224+ if ( _isDisposed ) throw new ObjectDisposedException ( GetType ( ) . FullName ) ;
225+
210226 if ( ! InternalDevices . Remove ( device ) ) return false ;
211227
212228 try { OnDevicesChanged ( DevicesChangedEventArgs . CreateDevicesRemovedArgs ( device ) ) ; } catch { /* we don't want to throw due to bad event handlers */ }
@@ -241,12 +257,26 @@ public virtual void Throw(Exception ex, bool isCritical = false)
241257 protected virtual void OnDevicesChanged ( DevicesChangedEventArgs args ) => DevicesChanged ? . Invoke ( this , args ) ;
242258
243259 /// <inheritdoc />
244- public virtual void Dispose ( )
260+ public void Dispose ( )
245261 {
246- Reset ( ) ;
262+ if ( _isDisposed ) return ;
263+
264+ try
265+ {
266+ Dispose ( true ) ;
267+ }
268+ catch { /* don't throw in dispose! */ }
247269
248270 GC . SuppressFinalize ( this ) ;
271+
272+ _isDisposed = true ;
249273 }
250274
275+ /// <summary>
276+ /// Disposes the object and frees all resources.
277+ /// </summary>
278+ /// <param name="disposing"><c>true</c> if explicitely called through the Dispose-Method, <c>false</c> if called by the destructor.</param>
279+ protected virtual void Dispose ( bool disposing ) => Reset ( ) ;
280+
251281 #endregion
252282}
0 commit comments