#if SIMPLSHARP using Crestron.SimplSharp; namespace ICD.Common.Utils { /// /// CCriticalSection tends to get disposed before the parent is done with it. /// This class is an attempt to gracefully handle the ObjectDisposedExceptions we see on /// program termination, ocassionally causing the program to restart instead of stop. /// public sealed partial class SafeCriticalSection { private readonly CCriticalSection m_CriticalSection; /// /// Constructor. /// public SafeCriticalSection() { m_CriticalSection = new CCriticalSection(); } #region Methods /// /// Block until ownership of the critical section can be obtained. /// public void Enter() { if (m_CriticalSection == null || m_CriticalSection.Disposed) return; m_CriticalSection.Enter(); } /// /// Release ownership of the critical section. /// public void Leave() { if (m_CriticalSection == null || m_CriticalSection.Disposed) return; m_CriticalSection.Leave(); } /// /// Attempt to enter the critical section without blocking. /// /// /// True, calling thread has ownership of the critical section; otherwise, false. /// public bool TryEnter() { if (m_CriticalSection == null || m_CriticalSection.Disposed) return false; return m_CriticalSection.TryEnter(); } #endregion } } #endif