Controls access by other processes to all or part of a file opened using the Open statement.
Lock[%] # n [, recordrange]
Unlock[%} # n[, recordrange]
err = Lock[%]( # n [, recordrange] )
err = Unlock[%]( # n[, recordrange] )
n | : iexp (0..511) |
recordrange | : recnumber | [start] To end |
err | : iexp |
The Lock and Unlock statements/functions are used in environments where several processes might need access to the same file. With the command Lock you can lock a part (i.e. one record of a file) of a previously opened file. Lock and Unlock statements are always used in pairs. The arguments to Lock and Unlock must match exactly.
The following applies to both Lock and UnLock (Random files start counting at 1, unless the file was opened with Based 0. For sequential files recnumber is the byte number.):
Lock #n
Locks the entire file.
Lock #n, offset, count
Offset is the first byte to lock, while count holds the number of bytes to lock from that point.
Lock #n, recnumber
Locks a record with the specified number in a random file. Random files start counting at 1, unless the file was opened with Based 0. For sequential files recnumber is the byte number.
Lock #n, recnumstart To recnumend
Locks a range of records or bytes.
Lock #n, To recnumend
All records from the first record to the end of the range (recnumend) are locked (or unlocked).
When a locked file is accessed by another process, the (Un)Lock commands generate a runtime error. To prevent your application from crashing these statements should be enclosed in a Try/Catch block. The runtime errors are returned as function return values when (Un)Lock is used as a function.
Lock and UnLock can take large values to access large files; GFABASIC also contains Lock% and UnLock% commands/functions which work in exactly the same way but take Int32 values only for compatibility with older file systems.
Local a$, ret%
// Create the test file
Open "c:\Test.Dat" for Output As # 1
Write # 1, String$(200, "A")
Close # 1
// Open the test file for shared access
Open "c:\Test.Dat" for Input Shared As # 1
// Lock bytes 0 to 100 of the file
ret% = Lock(# 1, 0, 100)
OpenW 1
If ret% = 0 // If there is no runtime error
Print "This file has exclusive access"
Flush # 1 // Flush the file cache
a$ = Input$(100, # 1) // Read in the first 100 bytes of the file
Print "Extracted info: "; a$ // Print data
Unlock # 1, 0, 100 // Unlock previously locked bytes (now: no error handling if error returned)
Print "Now everyone can connect again."
Else
Print "Error #"; ret%; " during locking" // If there was a runtime error when trying to lock the file
EndIf
Close # 1
// Clear test file
Kill "c:\Test.Dat"
In a multitasking environment problems often arise with simultaneous access of the same file. To solve this problem easily, open a file with the command Open, but without the option Shared. This will open the file exclusively, and all other applications have to wait until the file is closed.
Otherwise, to allow multiple applications access to a file it should be opened with the Shared flag. When using (Un)Lock an application can lock the part of the file that it needs to access but not for longer than is necessary.
{Created by Sjouke Hamstra; Last updated: 22/06/2020 by James Gaite}