'Classes/ActiveBasic/Windows/CriticalSection.ab #require Namespace ActiveBasic Namespace Windows Class CriticalSection Inherits System.IDisposable Public Sub CriticalSection() disposed = 0 InitializeCriticalSection(cs) End Sub Sub ~CriticalSection() Dispose() End Sub Override Sub Dispose() If InterlockedIncrement(disposed) = 0 Then DeleteCriticalSection(cs) End If End Sub Function Enter() As CriticalSectionLock Return New CriticalSectionLock(cs) End Function Private cs As CRITICAL_SECTION disposed As Long End Class Class CriticalSectionLock Inherits System.IDisposable Public Sub CriticalSectionLock(ByRef cs As CRITICAL_SECTION) pcs = VarPtr(cs) EnterCriticalSection(cs) End Sub Sub ~CriticalSectionLock() Dispose() End Sub Sub Leave() Dispose() End Sub Override Sub Dispose() Dim p = InterlockedExchangePointer(ByVal VarPtr(pcs), 0) If p <> 0 Then LeaveCriticalSection(ByVal p) End If End Sub Private pcs As *CRITICAL_SECTION End Class End Namespace 'Windows End Namespace 'ActiveBasic