Locking files in VB6
-
:rose::rose: How do lock a file in VB6 without hardcoding to append a .lock ext to the file name? How do i unlock? Pls help
-
:rose::rose: How do lock a file in VB6 without hardcoding to append a .lock ext to the file name? How do i unlock? Pls help
What do you mean by lock? It doesn't sound like the standard definition of the term. Besides, using .lock just changes the file extension, not any exclusive rights to the file. Are you trying to get exclusive access to the file? If so, then your Open statement should look something like:
Open "filename.ext" For Binary Access Read Lock Read Write As #1
Use whatever File Mode and Access you want, but the important part is the Lock modifier. RageInTheMachine9532
-
What do you mean by lock? It doesn't sound like the standard definition of the term. Besides, using .lock just changes the file extension, not any exclusive rights to the file. Are you trying to get exclusive access to the file? If so, then your Open statement should look something like:
Open "filename.ext" For Binary Access Read Lock Read Write As #1
Use whatever File Mode and Access you want, but the important part is the Lock modifier. RageInTheMachine9532
hi RageInTheMachine9532, you r right i dint mean the std locking of files. i want many app instances to share a folder of .tif files. if app1 is using a file, it renames it with a .lock ext so app2 knows it is being modified by checking the ext. before an app exits, it renames files its been using from .lock to .tif. problem is if app1 crashes, the files remain with a .lock ext n neither app1 nor app2 can use them! how do i handle this? i mean without resorting to the normal windows locking.. :rose::rose:
-
hi RageInTheMachine9532, you r right i dint mean the std locking of files. i want many app instances to share a folder of .tif files. if app1 is using a file, it renames it with a .lock ext so app2 knows it is being modified by checking the ext. before an app exits, it renames files its been using from .lock to .tif. problem is if app1 crashes, the files remain with a .lock ext n neither app1 nor app2 can use them! how do i handle this? i mean without resorting to the normal windows locking.. :rose::rose:
Well, the locking is the only "sure" way to control file access and you don't have to deal with renaming stuff back when you crash. The problem you have is this: When AppInstance1 checks for the availability of File1.tif, it's there. The next step, is for it to rename File1.tif to File1.locked. But, What if AppInstance2 found that File1.tif was available at (nearly) the same time, say, between AppInstance1 finding the file and renaming it? In this case, BOTH instances will find the file, but only one will succeed in renaming it. How is this case handled? It's too much of a pain to write your own file locking scheme when Windows has locking built in. It also has the advantage of being "thread safe", where your custom scheme does not. RageInTheMachine9532