Link to home
Start Free TrialLog in
Avatar of rjef
rjefFlag for United States of America

asked on

update counter file using more than 1 program in VB6

i want to make a program that will run on several pc's made in vb6.  i need it to have a button and when the button is clicked the program will go to a file on a pc and get the value in it and add 1 to it then save it again into that file. (so there is always just one number in the file)  I want all the programs to do the same thing to the same file.  What do you suggest i do to keep the counting file from not losing data or missing a count?
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rjef

ASKER

i have this in VB.  Ran it in 5 different instances but did not wind up with 500 on the counter.

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
Randomize
For hhh = 1 To 100
Label1.Caption = hhh
Label1.Refresh
sleepnumber = Int((1000 - 0 + 1) * Rnd + 0)
Sleep (sleepnumber)
Open "C:\FileToOpen\FileToOpen.txt" For Input As #1
Line Input #1, countit
Close #1
countit = countit + 1
Open "C:\FileToOpen\FileToOpen.txt" For Output As #1
Print #1, countit
Close #1
Next hhh
End Sub
Avatar of rjef

ASKER

i added this but would like it cleaned up a bit
this seems to work

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Randomize
For CountThem = 1 To 100
    DoEvents
    Label1.Caption = CountThem
    Label1.Refresh
    SleepNumber = Int((1000 - 0 + 1) * Rnd + 0)
    Sleep (SleepNumber)
     DoEvents
        MyFile = Dir("c:\FileToOpen\IsItBusy")
        If MyFile = "" Then
            Open "C:\FileToOpen\IsItBusy" For Output As #2
            Close #2
            Open "C:\FileToOpen\FileToOpen.txt" For Input As #3
                Line Input #3, countit
            Close #3
            countit = countit + 1
                Open "C:\FileToOpen\FileToOpen.txt" For Output As #1
                    Print #1, countit
                Close #1
                Kill "C:\FileToOpen\IsItBusy"
            Else
                CountThem = CountThem - 1
            End If
Next CountThem
End Sub

Open in new window