[Gambas-user] How to lock a file

Benoit Minisini gambas at ...1...
Fri Sep 26 23:51:57 CEST 2008


On vendredi 26 septembre 2008, Almanova Sistemi wrote:
> I need to lock the file to avoid writing by another user in multiuser
> environment
>

Sorry for that, the documentation is completely false. But who wrote it? :-)

The LOCK instruction does not lock a specific stream not a stream, but instead 
use a specific path to create a global system lock.

You use it this way:

DIM hFile AS File

TRY hFile = LOCK "~/lock"
IF ERROR THEN
  PRINT "Already locked by something else!"
ELSE
  PRINT "Got locked!"
  ...
  UNLOCK hFile
ENDIF

Do not rely on the lock file contents. It is truncated to zero byte when the 
lock is acquired.

Regards,

-- 
Benoit Minisini




More information about the User mailing list