diff --git a/src/PepperDash.Essentials.Core/Comm and IR/ComPortController.cs b/src/PepperDash.Essentials.Core/Comm and IR/ComPortController.cs index 8b28d23b..0bc71c62 100644 --- a/src/PepperDash.Essentials.Core/Comm and IR/ComPortController.cs +++ b/src/PepperDash.Essentials.Core/Comm and IR/ComPortController.cs @@ -38,6 +38,8 @@ namespace PepperDash.Essentials.Core ComPort Port; ComPort.ComPortSpec Spec; + private readonly object _deactivateLock = new object(); + private bool _isDeactivated; /// /// Constructor @@ -148,7 +150,9 @@ namespace PepperDash.Essentials.Core void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType) { if (programEventType == eProgramStatusEventType.Stopping) + { Deactivate(); + } } /// @@ -157,6 +161,16 @@ namespace PepperDash.Essentials.Core /// public override bool Deactivate() { + lock (_deactivateLock) + { + if (_isDeactivated) + return true; + + _isDeactivated = true; + } + + CrestronEnvironment.ProgramStatusEventHandler -= CrestronEnvironment_ProgramStatusEventHandler; + if (Port == null) return true;