Lock(), RLock(), Set MultiLocks, Set("MultiLocks"), Unlock
These functions and commands allow locking and unlocking of one or more records. It is only by locking a record that the application can be sure that it is viewing the actual data on disk. MULTILOCKS allow more than one record in a table to be locked at one time; this is essential for table buffering. With the introduction of buffering in Visual FoxPro, explicit record locking is needed far less frequently.
Usage |
lSuccess = RLOCK | LOCK ( [ [ cRecordList, ] nWorkArea | cAlias ] ) |
Parameter |
Value |
Meaning |
cRecordList |
Character |
A comma-separated list of the record numbers to be locked. |
nWorkArea |
Numeric |
Specifies the work area in which the lock should be placed. |
Omitted |
If cAlias is also omitted, place the lock in the current work area. |
|
cAlias |
Character |
Specifies the alias of the table receiving the lock. |
Omitted |
If cAlias is also omitted, the lock takes place on the current table. |
|
lSuccess |
.T. |
Lock(s) were successfully placed. |
.F. |
The lock(s) failed because another user has the record or table locked, or the work area has no table open, or the record numbers do not exist. If the alias is not valid, the error "Alias not found" is generated. |
Issuing RLOCK() with a list of multiple records to lock returns .T. if the locks can be placed—even if MULTILOCKS is set OFF! But only the last record ends up locked—each is locked in sequence, as if you had issued separate RLOCK()s for each record, and then each is unlocked automatically as the next record is locked. This is dumb! RLOCK() should always return .F. if a list of records is supplied, and MULTILOCKS is not ON. |
Example |
? RLOCK("82,93,75",ALIAS()) && locks 3 records |
Usage |
SET MULTILOCKS ON | OFF cOnOrOff = SET( "MULTILOCKS" ) |
Contrary to the Help file, setting MULTILOCKS OFF unlocks all records in the table, if more than one record is locked, but leaves an individual lock if only one is in place. It also leaves individual locks in place in other work areas, although it does clear multiple locks on those tables. |
Usage |
UNLOCK [ RECORD nRecord ] [ IN nWorkArea | cAlias ] | [ ALL ] |
Parameter |
Value |
Meaning |
nRecord |
0 |
Unlocks the table header. |
Integer |
Unlocks the specified record number. |
|
nWorkArea |
Integer |
Specifies the work area in which lock(s) are to be released. |
Omitted |
In cAlias is also omitted, unlocks records in the current work area. |
|
cAlias |
Character |
The alias of the table whose locks are to be released. |
Omitted |
If nWorkArea is also omitted, unlocks records in the current work area. |
UNLOCK and its relative RLOCK() show some distinctive inconsistencies—you lock records with a function, but unlock them with a command. You can specify multiple records to lock with a character string, comma-separated list, but you can unlock only an individual record or all of them—not a specified subset. While we're not campaigning for the ability to specify lists of records to unlock—we've never needed the function much—we do find the inconsistencies worth noting. |
Example |
UNLOCK ALL |