Page 1 of 1

Waiting for an exclusive access to hashed file

Posted: Mon Jan 19, 2009 12:38 pm
by yserrano
Hello all,

I am using the FILELOCK statement to ask for an exclusive access to a hashed file and it is working very well.

Code: Select all

TargetName = "TargetFile"
Open TargetName TO HashedTargetFile Then
     FILELOCK  HashedTargetFile
...
But, in case the file is locked by another user I would like my routine to wait some seconds for the resource and then to ask again , in a loop, until the file is available.
Have you implemented something like that before? Do you have any suggestions?
I checked the "ON ERROR" and "LOCKED" statements to see if I could use them, but did not find anything related.

Posted: Mon Jan 19, 2009 3:42 pm
by ray.wurlod
You found it, but did not realize it. The LOCKED clause is indeed what you want.

Code: Select all

$INCLUDE UNIVERSE.INCLUDE RECORDLOCKED.H
Equate MAXTRIES To 5    ; * maximum number of attempts
Equate NAPTIME To 1500  ; * 1.5 seconds

For Attempt = 1 To MAXTRIES
   FileLock HashedTargetFile
   Locked
      Nap NAPTIME
   End   ; * end of FileLock statement
While RecordLocked(HashedTargetFile, "DUMMY") <> LOCK$MY.FILELOCK
Next Attempt

* File lock could not be obtained.
If RecordLocked(HashedTargetFile, "DUMMY") <> LOCK$MY.FILELOCK
Then
   * Code here to handle "could not be file-locked".
End

Posted: Thu Jan 22, 2009 12:18 pm
by yserrano
I thought the FILELOCK function had an additional parameter to wait "automatically" a given time when the file was locked by another user/process.

I will use the code you provided, thank you.

Posted: Thu Jan 22, 2009 3:07 pm
by ray.wurlod
There is no "time" component on the FILELOCK statement. Perhaps you are confusing this with the task synchronization locks, which are locked by a LOCK statement, which does have a timeout capability.