| View previous topic :: View next topic |
| Author |
Message |
jwfv Valued Contributor

Joined: 19 Mar 2002 Posts: 422 Location: Beaufort, SC
|
Posted: Wed Feb 18, 2004 10:59 pm Post subject: inifile help |
|
|
I have a program that uses sequential "quote" numbers that are assigned to customers as they are entered into the program.
As a customer is saved, the program reads the next value from an .INI file and then increments that value in the .INI file. Here is the code:
| Code: |
%%thisquote = @iniread(quote,quotenum)
inifile write, quote, quotenum, @sum(%%thisquote,1)
|
Here is the problem:
Some very busy users are reporting that they are getting duplicate numbers assigned. I have tested it on my network, and if two people save at the same time, there are problems.
Basically, the operation doesn't happen fast enough, and there is no file locking of course, so one reads, then the other reads, then the first one changes it, and they both have the same number.
I'm just kind of fishing for ideas - anyone have any? Are there any locking methods I could use? (I'd rather not change the whole method of keeping the quote numbers, etc.)
Thanks. _________________ Joe Floyd |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Wed Feb 18, 2004 11:04 pm Post subject: |
|
|
Locking is a very simple process if you want to implement it. Basically you
can create a text file in a network share with read/write access for your
program. Your program can check the content of the file to see if it is locked.
For example, if the text file contains "0", then the file is available
to read/write to. If an instance of your program has written the content
to "1", then your program should recognize that as meaning the file is
locked. When the instance of the program that locked the file is done using
the file, it should write the content back to "0".  _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
SnarlingSheep Professional Member


Joined: 13 Mar 2001 Posts: 759 Location: Michigan
|
Posted: Wed Feb 18, 2004 11:12 pm Post subject: |
|
|
| FreezingFire wrote: | Your program can check the content of the file to see if it is locked.For example, if the text file contains "0", then the file is available
to read/write to. |
Then you could run into the same problem.. 2 users read this file at the same time and it's 0, then they both can read/write to the ini file. _________________ -Sheep
My pockets hurt... |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Wed Feb 18, 2004 11:13 pm Post subject: |
|
|
I guess that's right. Then I'm not sure exactly what to do.  _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Thu Feb 19, 2004 12:23 am Post subject: |
|
|
If you can use regular file access instead of @iniread()
you might try VDSug.dll.
It doesn't allow sharing when a file is opened in WRITE
or READ|WRITE mode.
Cheers, Mac  _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
jwfv Valued Contributor

Joined: 19 Mar 2002 Posts: 422 Location: Beaufort, SC
|
Posted: Thu Feb 19, 2004 2:32 pm Post subject: |
|
|
Thanks for the suggestions - I'll keep working on it. I guess the file locking using VDSug or some other method is the best idea so far, but I'm not looking forward to changing the code around. I'm going to mull it over ... _________________ Joe Floyd |
|
| Back to top |
|
 |
DavidR Contributor

Joined: 05 Aug 2003 Posts: 83 Location: Bethel Pennsylvania U.S.A.
|
Posted: Thu Feb 19, 2004 3:45 pm Post subject: Re: inifile help |
|
|
| jwfv wrote: | If two people save at the same time, there are problems.
Basically, the operation doesn't happen fast enough, and there is no file locking of course, so one reads, then the other reads, then the first one changes it, and they both have the same number.
I'm just kind of fishing for ideas - anyone have any? Are there any locking methods I could use? (I'd rather not change the whole method of keeping the quote numbers, etc.)
Thanks. |
I think I can help! I do this all the time.
My approach is to have my application generate a "flag" file of the form <some_random_number.flg>. I write this file in the same directory as my INI file. I then modify the INI file. When I'm finished I delete the .flg file.
So during normal operations, when I'm ready to modify the INI, I first check to see if any .flg files exist. If so, wait a second or two and check again. When no .flg files exist I know that nobody else is accessing the INI file and I write my .flg file. So, you might ask "what happens if 2 .flg files are written at once"? This is the equivalent of a "network" collision. Before I actually change my INI file I check to see that only one .flg file exists. If more than one exists I delete the one I wrote and wait for a random amount of time before trying again. Bottom line is, my application is not allowed to modify the INI file unless only my .flg file exists. This approach works every time.
There is one additional error handling procedure that needs to be implemented. In a very rare situation, an application could write a .flg file and crash or the network goes down before it has a chance to delete it. This would lock out all the other users. To prevent this, when the application finds an existing .flg file and that file continues to exist after several "wait and check" sequences (longer than it could possibly take to update the INI file) it assumes that it is an "orphan" file and deletes it which allows normal operation to continue.
Sorry if the explanation was kind of convoluted but I've found it to be a simple and straightforward approach without getting in file locking. If an application "locks" a file and then crashes before file is unlocked, there needs to be some method to unlock it or the whole system stops until you manually intervene.
Hope this helps.........
.........David |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Thu Feb 19, 2004 4:17 pm Post subject: |
|
|
| jwfv wrote: | | Thanks for the suggestions - I'll keep working on it. I guess the file locking using VDSug or some other method is the best idea so far, but I'm not looking forward to changing the code around. I'm going to mull it over ... |
Another way ya might avoid re-writing the whole program...
Open the file with VDSug (leave it open) and write it to a
temp INI file. Use your existing code (@iniread etc.) to
modify the temp file, then write it back to the original file
with VDSug and close it.
Cheers, Mac  _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
jwfv Valued Contributor

Joined: 19 Mar 2002 Posts: 422 Location: Beaufort, SC
|
Posted: Thu Feb 19, 2004 4:38 pm Post subject: |
|
|
Thanks for the great suggestions.
Mac - a question on VDSug:
To make sure I have your suggestion straight in my head -
I would use the command UG FileOpen to open the .ini file in what mode? I would then use FILE COPY command in VDS to copy to another .ini file, make my changes, then copy back? Or would I need to use VDSUG since that is the DLL that has it locked? _________________ Joe Floyd |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Thu Feb 19, 2004 4:47 pm Post subject: |
|
|
You'd have to use VDSug to write to the temp file,
but there is a FILEREAD command that allows you
to read to an internal buffer (and FILEWRITE from
it) without returning the data to VDS. The buffer
size is adjustable if your INI file is over 4k.
There are help files at the link in my sig in both
txt and hlp format, and I'll be glad to help ya if
you have any questions.
Cheers, Mac  _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
DavidR Contributor

Joined: 05 Aug 2003 Posts: 83 Location: Bethel Pennsylvania U.S.A.
|
Posted: Thu Feb 19, 2004 4:48 pm Post subject: |
|
|
| jwfv wrote: | Thanks for the great suggestions.
|
However you decide to do it you will still need to integrate additional error handling to deal with the inevitable "collisions". For example, what if your application "locks" the file and another user attempts to open it. There needs to be a graceful way to go into a "wait" state and try again. If for some reason the file doesn't get "unlocked" or renamed back to the original you will need a way to deal with that too. |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Thu Feb 19, 2004 4:52 pm Post subject: |
|
|
On second thought, you prolly could copy the file
while it's open if you prefer. But this could negate the
file lock effect if a second instance of your prog fails
to check if the file is open, and just copies it anyway.
Cheers, Mac  _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
jwfv Valued Contributor

Joined: 19 Mar 2002 Posts: 422 Location: Beaufort, SC
|
Posted: Thu Feb 19, 2004 5:00 pm Post subject: |
|
|
Thanks guys -
Yeah, I'll definitely need to have a good routine in there to handle locked files, which I haven't had to deal with in this program yet. But I think I'll try one of these methods out.
Thanks again - _________________ Joe Floyd |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Thu Feb 19, 2004 5:06 pm Post subject: |
|
|
BTW, VDSug's FILEOPEN command sets "OK" to FALSE
if the file/device/etc. cannot be opened, so error checking
should be relatively painless.
Cheers, Mac  _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
|