How to make a resource lock
Keywords: Error 33 Locking Violation lockfile resource lock
Question:
I'm getting the Windows error 33 Locking Violation on different machines at different times.I have an ini file on the network that I want multiple users to be able to read/write to. What do I need to do to prevent share violations and/or corruption of data when multiple users try to use that file at the same time. I am using iniwritepvt and inireadpvt functions and am not sure how they handle file open/closes. Thanks!
Answer:
If your script updates files on a server, and a number of people might be running the script at any given time, then you will need to lock those files, so that only one person can use them at a time. The lock prevents everyone from stomping all over the files. First figure out what files are being updated, and then use the following script to figure out if the file is open.It's also a good idea to mark your WinBatch EXEs and DLLs as Read-only, so that they don't get totalled.
For multiple users on the network accessing the same ini file with ini operations requires a lot of care.
Here are two very similar examples:
- A Resource lock has to be set up for the ini file.
- And you must flush the current machines buffers since by default windows is allowed to cache ini files.
Example #1:
Netdir="N:\stuff\" inifile=strcat(netdir,"yourini.ini") lockfile=strcat(netdir,"lock.txt") lock=0 while lock ==0 Errormode(@off) lock=FileOpen(lockfile,"WRITE") Errormode(@cancel) if lock!=0 then break TimeDelay(0.5) endwhile iniWritePvt("section","key",123,inifile) iniwritepvt("","","",inifile) FileClose(lock)
Example #2:
;Lock Code example. ;Define lock lockfile="X:\someplac\lockfile.lck" ;Lock ini file ErrorMode(@off) for x=1 to 10 fh=FileOpen(lockfile,"WRITE") if fh==0 TimeDelay(2) else break endif next ErrorMode(@CANCEL) ;Don't even consider removing this line if fh==0 Message("Ooopsie","Could not obtain application lock") exit endif ; lock is secured ;Do your stuff here z=IniReadPvt(... z=z+1 IniWritePvt(... IniWritePvt("","","",inifilename);force ini data to disk. ;Release Lock FileClose(fh)Question (continued):
In the sample code above for locking a file so other users on the network wouldn't make changes while you make your own changes, in the statement:FileOpen( filename, "WRITE" )does this create a new file, and if the file exists, overwrite it? I tried to open the file with APPEND. This wouldn't zero the file, but now I was not able to do the IniReadPvt() because the file was locked???The IniReadPvt and IniWritePvt functions work fine if I don't open/lock the file, but they don't work if the file is opened first.
Here's my code:
;Lock ini file ErrorMode(@off) fh=FileOpen(lockfile,"WRITE") ErrorMode(@CANCEL) if fh==0 Message("Ooopsie","Could not lock") exit endif ; lock is secured ;Do your stuff here z=IniReadPvt(... z=z+1 IniWritePvt(... ;Release Lock FileClose(fh)Answer:
You don't lock the INI file, you lock a different file that you logically associate with the INI file. Pick a name like INIFILE.LCK.And before you clear the lock (with the release lock of FileClose(fh)) do a:
IniWritePvt("","","",youinifile) TimeDelay(3)So here's the general concept:If WinBatch is the ONLY program that references the file...
- Lock a DIFFERENT file in the same directory.
- Do your ini operations.
- If you do write the file, do a:
IniWritePvt("","","",inifilename)to force a dump of the local machine cache of the ini file to disk.
- Unlock the lock file.
Article ID: W13169Filename: Make a resource LOCK.txt