LockSemaphore() ![]()
Performs a Semaphore Lock.
Syntax
LockSemaphore(<nLock>, <[nTimeOut]>) --> lSuccess
Arguments
<nLock> --> The lock ID. This must be a number between 1 and 10.
<nTimeOut> --> An optional time-out value in seconds. If a semaphore lock is not achieved within this time frame, the function exits with a FALSE return value. If a time-out value is not specified, LockSemaphore() will continue to attempt the lock until successful.
Returns
Returns FALSE if the attempt to semaphore lock the system was not successful. Otherwise TRUE is returned.
Description
This function performs a system semaphore lock to ensure that no other task or process can execute the same set of commands after the lock is applied. This is useful when you need to ensure that no other task or process on your computer or network will run the same script at exactly the same time.
For example, if you need to increment a database field and also guarantee that the value of that database field was truely unique, a semaphore lock could be used to achieve this.
The
lock should be released as soon as possible, as it will prevent other applications that execute the same
script from proceeding forward, until the lock is released. Never place pauses, messages boxes or any
other element that requires user interaction, after successfully achieving a semaphore lock until after
the semaphore lock is released.
An
UnlockSemaphore() call with the same lock ID must promptly follow a LockSemaphore() call in all your scripts.
Otherwise your scripts may appear to 'hang' indefinitely if executed from other computers or threads of
execution.
Example
* A semaphore lock ensures that no two
* computers or threads of execution will
* attempt to copy cMyFile1 to cMyFile2
* at exactly the same time.
LockSemaphore(1)
CopyFile(cMyFile1, cMyFile2)
UnlockSemaphore(1)
____________________________
Related Topics: