diff --git a/src/devices/Tca955x/Tca955x.cs b/src/devices/Tca955x/Tca955x.cs index 53978f53ad..bf98169bb8 100644 --- a/src/devices/Tca955x/Tca955x.cs +++ b/src/devices/Tca955x/Tca955x.cs @@ -646,24 +646,27 @@ protected override bool IsPinModeSupported(int pinNumber, PinMode mode) => /// protected override void Dispose(bool disposing) { - _controller?.UnregisterCallbackForPinValueChangedEvent(_interrupt, InterruptHandler); - _interruptPending = false; + if (disposing) + { + _controller?.UnregisterCallbackForPinValueChangedEvent(_interrupt, InterruptHandler); + _interruptPending = false; - // Make a copy of the task refernce to avoid a race condition - // between checking it for null and then waiting on it. - var localinterruptProcessingTask = _interruptProcessingTask; - localinterruptProcessingTask?.Wait(); + // Make a copy of the task reference to avoid a race condition + // between checking it for null and then waiting on it. + var localinterruptProcessingTask = _interruptProcessingTask; + localinterruptProcessingTask?.Wait(); - if (_shouldDispose) - { - _controller?.Dispose(); - } + if (_shouldDispose) + { + _controller?.Dispose(); + } - _controller = null; + _controller = null; - _busDevice?.Dispose(); + _busDevice?.Dispose(); + } - base.Dispose(true); + base.Dispose(disposing); } }