[Gambas-user] How to lock a file
Rolf-Werner Eilert
eilert-sprachen at ...221...
Tue Oct 7 10:47:02 CEST 2008
For my apps in Gambas, I use lockfiles, not folders, to lock a certain
file. Experience told me that "sometimes" when two users/processes want
to access the same file, these may overlap. So I added an identification
method and a time delay in case of locks. This is what it looks like
(kinda prototyped):
WHILE Exist(theLockFile) THEN
OPEN theLockFile FOR READ ...
LINE INPUT firstLine
CLOSE ...
IF firstLine = myOwnName THEN 'old lock file from myself:
KILL theLockFile 'may be deleted, then proceed
ELSE 'lock file from someone else:
WAIT one second
WEND
'now first of all, write my own lockfile
'then begin reading the file I want
'finally delete lockfile
This is how this looks in practice:
WHILE Exist(pfad &/ "klassenbuch.block")
OPEN pfad &/ "klassenbuch.block" FOR READ AS #dtnr
LINE INPUT #dtnr, t$
CLOSE #dtnr
IF t$ = system.User THEN
KILL pfad &/ "klassenbuch.block"
ELSE
WAIT 1
END IF
WEND
OPEN pfad &/ "klassenbuch.block" FOR WRITE CREATE DIRECT AS #dtnr
PRINT #dtnr, system.User
CLOSE #dtnr
OPEN karteiVerz &/ jg &/ "klassenbuch" FOR READ AS #dtnr
and so on. While I am writing this email, I find that this has one big
drawback: If the lock file was made by someone else and that app
crashed, my program will wait forever for the lockfile to disappear. So
one should add a counter, e. g. 5 seconds, to stop waiting.
Regards
Rolf
nando schrieb:
> In some applications, I create a folder as a lock.
> The existance of the folder indicates 'locked'
> It works very well and is used through Samba to share
> the lock on both the linux and windows side.
> -Fernando
>
>
> ---------- Original Message -----------
> From: Benoit Minisini <gambas at ...1...>
> To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
> Sent: Fri, 26 Sep 2008 23:51:57 +0200
> Subject: Re: [Gambas-user] How to lock a file
>
>> 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