par billbock » 20 Fév 2007 à 15:25
- Code : Tout sélectionner
%REM
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Title:
clsCodeLock
Overview:
Helper class to create a code lock that can prevent another process/agent executing the
same code at the same time. The lock will clear itself even if you forget to unlock/disable it.
This object can also be re-used by using the setLockName method.
Caveats:
Lotus Notes R5+ only.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%END REM
Public Class clsCodeLock
Public LastError As String
Public isValid As Integer
Public LockName As String
Public LockID As Integer
Private hasLock As Integer
Private unlockLock As Integer
Private destroyLock As Integer
Sub New(Byval sLockName As String)
Call setLockName(sLockName)
End Sub
Function setLockName(Byval sLockName As String) As Integer
' Allow the object to be reused by allowing the
' lock name to be reset.
' Release any existing locks referenced by this object.
Call disableLock()
Me.LockName = sLockName
' If a lock with the supplied name already exists that will be
' returned otherwise a new lock is created with the supplied name.
' CreateLock will return an error if the platform does not support
' locks or if there is insufficient shared memory.
On Error Resume Next
Me.LockID = Createlock(Me.LockName)
If Err > 0 Then
Me.LastError = Error & " #" & Cstr(Err)
Else
Me.isValid = True
End If
setLockName = Me.isValid
End Function
Function enableLock As Integer
If Me.isValid Then
' This section will not return until the user has access to the
' lock, thus allowing the user to access the sychronized area.
Me.hasLock = Codelock(Me.LockID)
enableLock = Me.hasLock
End If
End Function
Function disableLock As Integer
If Me.isValid And Me.hasLock Then
' Release the lock if we have one
If Not Me.unlockLock Then
Me.unlockLock = Codeunlock(Me.LockID)
End If
If Not Me.destroyLock Then
' Delete the reference to the lock
Me.destroyLock = Destroylock(Me.LockID)
End If
' Do we still have a lock?
Me.hasLock = Me.unlockLock And Me.destroyLock
disableLock = Me.hasLock
' The lock object may now be invalid as it has been released
' and destroyed.
Me.isValid = Not disableLock
End If
End Function
Function checkLock As Long
' Get the number of agents waiting for the the specified
' lock, plus 1.
If Me.isValid And Me.hasLock Then
checkLock = Codelockcheck(Me.LockID)
End If
End Function
Sub Delete
' Delete is called whenever the object is destroyed
' so we may need to clean up. The lock is disabled in this
' section if we forget to call "disableLock" elsewhere in
' our code.
If Me.isValid Then
Call Me.disableLock()
End If
End Sub
End Class