forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


inifile help

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  
Author Message
jwfv
Valued Contributor
Valued Contributor


Joined: 19 Mar 2002
Posts: 422
Location: Beaufort, SC

PostPosted: Wed Feb 18, 2004 10:59 pm    Post subject: inifile help Reply with quote

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
View user's profile Send private message
FreezingFire
Admin Team


Joined: 23 Jun 2002
Posts: 3508

PostPosted: Wed Feb 18, 2004 11:04 pm    Post subject: Reply with quote

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". Smile

_________________
FreezingFire
VDSWORLD.com
Site Admin Team
Back to top
View user's profile Send private message Visit poster's website
SnarlingSheep
Professional Member
Professional Member


Joined: 13 Mar 2001
Posts: 759
Location: Michigan

PostPosted: Wed Feb 18, 2004 11:12 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
FreezingFire
Admin Team


Joined: 23 Jun 2002
Posts: 3508

PostPosted: Wed Feb 18, 2004 11:13 pm    Post subject: Reply with quote

I guess that's right. Then I'm not sure exactly what to do. Confused
_________________
FreezingFire
VDSWORLD.com
Site Admin Team
Back to top
View user's profile Send private message Visit poster's website
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Feb 19, 2004 12:23 am    Post subject: Reply with quote

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. Wink

Cheers, Mac Smile

_________________
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
View user's profile Send private message Send e-mail
jwfv
Valued Contributor
Valued Contributor


Joined: 19 Mar 2002
Posts: 422
Location: Beaufort, SC

PostPosted: Thu Feb 19, 2004 2:32 pm    Post subject: Reply with quote

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
View user's profile Send private message
DavidR
Contributor
Contributor


Joined: 05 Aug 2003
Posts: 83
Location: Bethel Pennsylvania U.S.A.

PostPosted: Thu Feb 19, 2004 3:45 pm    Post subject: Re: inifile help Reply with quote

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
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Feb 19, 2004 4:17 pm    Post subject: Reply with quote

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. Wink

Cheers, Mac Smile

_________________
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
View user's profile Send private message Send e-mail
jwfv
Valued Contributor
Valued Contributor


Joined: 19 Mar 2002
Posts: 422
Location: Beaufort, SC

PostPosted: Thu Feb 19, 2004 4:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Feb 19, 2004 4:47 pm    Post subject: Reply with quote

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 Smile

_________________
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
View user's profile Send private message Send e-mail
DavidR
Contributor
Contributor


Joined: 05 Aug 2003
Posts: 83
Location: Bethel Pennsylvania U.S.A.

PostPosted: Thu Feb 19, 2004 4:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Feb 19, 2004 4:52 pm    Post subject: Reply with quote

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 Smile

_________________
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
View user's profile Send private message Send e-mail
jwfv
Valued Contributor
Valued Contributor


Joined: 19 Mar 2002
Posts: 422
Location: Beaufort, SC

PostPosted: Thu Feb 19, 2004 5:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Feb 19, 2004 5:06 pm    Post subject: Reply with quote

BTW, VDSug's FILEOPEN command sets "OK" to FALSE
if the file/device/etc. cannot be opened, so error checking
should be relatively painless. Wink

Cheers, Mac Smile

_________________
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
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group