[V4RB] Open/Close trick Revisted
keatk at comcast.net
keatk at comcast.net
Fri Aug 20 16:24:28 CDT 2004
It turns out I did have to rely on LockFiles on the network at work to use it.
I need to be Xplatform so to get fully atomic operations I likely would have had resort to declares on each platform and I did not have the time to figure out how to do that...
So i tried to make it almost foolproof in RB code... Not sure if I succeeded, because with the low current usage collisons are likely to be rare for awhile. Anyway here is my lock file checking code. any comments world be appreciated:
Protected Function LockFileCheck() As Boolean
'At App Starup :
' Lockfile FolderItem is created
' AppGlobals.NetworkProcessID Is assigned
' AppGlobals.NetworkProcessID is created as follows:
' If System.NetworkInterfaceCount = 0 then
' NetworkProcessID = Format(Microseconds,"#.0000000000000000000000e")
' Else
' NetworkProcessID = System.GetNetworkInterface(0).MACAddress + Format(Microseconds,"#.0000000000000000000000e")
' End if
Dim Now As New Date
Dim OutStream as TextOutputStream, InStream as TextInputStream
Dim LockID, MyID AS String
If AppGlobals.NetworkProcessID = "" Then AppGlobals.MakeNetworkProcessID
MyID = AppGlobals.NetworkProcessID
If LockFile.Exists Then
If (Now.TotalSeconds - LockFile.CreationDate.TotalSeconds) > MaxLockFileAge_Sec Then
LockFile.Delete
Else ' then try to read it
InStream = LockFile.OpenAsTextFile
LockID = InStream.ReadLine
InStream.Close
If MyID <> LockID Then Return False 'Another process owns it
LockFile.Delete
End if
End if
If Not LockFile.Exists Then ' Create it
OutStream = LockFile.CreateTextFile
OutStream.WriteLine MyID
OutStream.Close
Else 'Another process beat this one to the punch
Return False
End if
' Try to read from the one just created just in case another process snuck in
InStream = LockFile.OpenAsTextFile
LockID = InStream.ReadLine
InStream.Close
Return MyID = LockID
Exception ' File system does not allow simultaneous acess or file deleted after creation but before reading
Return false
End Function
------------------------------------
And I use it like so: from my VDatabase subclass
------------------------------------
Function Open() As Boolean
If Me.isOpen Then Return false
Dim theErrNo as integer
theErrNo = me.TryToOpen
If me.isOpen Then
TryingToOpen = False
Return true
End If
dbBusyDialog.ShowDialog(me, Location.Name, theErrNo) ' this creates a timer that calls
' TryToOpen
If me.isOpen Then
Return True
Else
Quit
End if
End Function
---------------------
Protected Function TryToOpen() As Integer
Dim DidOpen as boolean, Err as Integer
If Not TryingToOpen Then dbOpeningDialog.Display(Location.Name)
isOpen = False
If me.LockFileCheck Then
DidOpen= Super.Open(Location)
if DidOpen and me.ErrNumber = 0 Then
isOpen = True
err = 0
Elseif Not DidOpen AND me.ErrNumber = 0 then
Err = -666
Else
err = me.ErrNumber
End if
Else
Err = -999
End if
If Not TryingToOpen Then dbOpeningDialog.Close
Return Err
End Function
More information about the Valentina
mailing list