Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help! I'm trying to lock a file 1

Status
Not open for further replies.

m284679

Programmer
Apr 19, 2001
3
US
I inherited a FoxPro system that pulls in a number from one table, increments it, and creates a key based on the incremented number. The problem is that it doesn't have any locking whatsoever and duplicate keys are being created. I need to lock the table so only the first user can read and write to the table during this process. If another user tries to access the table, I'd like the system to keep trying to get a lock on it until the table is free again. This is the first time I've used FoxPro, so any help would be greatly appreciated!!
Thanks!
 
Simply put, try this. Then check into the SET REPROCESS, etc. This assumes your are positioned on both current records you need to update.

SELECT keyfile
IF RLOCK() &&... record lock
REPLACE num WITH num + 1
SELECT datafile
IF RLOCK()
REPLACE key_num with keyfile.num
*... other replaces etc.
ENDIF
UNLOCK ALL
ELSE
WAIT WINDOW 'Someone else is updating number...' TIMEOUT 2
ENDIF

Dave S.
 
BTW, rather than, or in addition to a thank you in words it's nice to click the 'let DSummZZZ know this post was helpful!' below his name. This puts a star next to his name in the given thread and lets the person browsing the forum know that that thread might have something useful in it. I'm sure that there are a lot of people who don't have time to read every message in every thread.

It's also kind of fun, if you're the competitive type, to try 'collecting' stars by being helpful.

Dave Dardinger
 
Hi Dave,

I don't mean to be impolite.
Just a matter of interest, will your program retry to lock the record
in the keyfile or will it just give the error message? If it does not
retry the record locking again, the update transaction will not be
completed. Should anything else be done???
 
I took no offense, I appreciate the note of thanks. A star is nice but I usually don't get enough online time to respond to a lot of posts so I could never gather enough stars to catch up to some of the other gurus in this forum. :)

I usually put the above code in a DO WHILE .T. loop if I absolutely need that record locked. This will try 10 times then loop:

SET REPROCESS TO 10
DO WHILE .T.
IF RLOCK() &&... record lock
REPLACE num WITH num + 1
SELECT datafile
IF RLOCK()
REPLACE key_num with keyfile.num
*... other replaces etc.
ENDIF
UNLOCK ALL
EXIT
ELSE
WAIT WINDOW 'Someone else is updating number...' TIMEOUT 2
ENDIF
ENDDO

For the SET REPROCESS command, I took this from the MSDN for Visual Studio:

SET REPROCESS TO nAttempts [SECONDS] | TO AUTOMATIC

TO nAttempts [SECONDS]

Specifies the number of times Visual FoxPro attempts to lock a record or file after an initial unsuccessful attempt. The default value is 0, the maximum value is 32,000.

SECONDS specifies that Visual FoxPro attempts to lock a file or record for nAttempts seconds. It's available only when nAttempts is greater than zero.

For example, if nAttempts is 30, Visual FoxPro attempts to lock a record or file up to 30 times. If you also include SECONDS (SET REPROCESS TO 30 SECONDS), Visual FoxPro continuously attempts to lock a record or file for up to 30 seconds.

TO AUTOMATIC

Specifies that Visual FoxPro attempts to lock the record or file indefinitely (equivalent to setting nAttempts to –2). This clause is similar to setting nAttempts to -1, except that it includes the facility to quit the attempt to lock a record or file.

If an ON ERROR routine isn't in effect and you press ESC in response to the system message, an appropriate alert appears (for example, "Record is in use by another"). If a function attempts to place the lock, the alert isn't displayed and the function returns false (.F.).

If an ON ERROR routine is in effect and ESC is pressed, the ON ERROR routine is executed. If a function attempts to place the lock, an ON ERROR routine isn't executed and the function returns false (.F.).

Dave S.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top