Link to home
Start Free TrialLog in
Avatar of isnoend2001
isnoend2001Flag for United States of America

asked on

calling a timer in a loop

how can i correct this code to run 12 times
The timer is only called in the first run and never gets called again.

Option Explicit
 Dim mlAmount As Long
 Dim iCnt As Long
 Dim miCounter As Integer



Sub Start()
    Dim i As Integer
    For i = 1 To 12
      iCnt = 1
    mlAmount = 10
    DoEvents
    Timer1.Interval = 60
    Timer1.Enabled = True
    Next
End Sub

Private Sub Timer1_Timer()
If iCnt <= mlAmount Then
    lblWinThisRun.Caption = iCnt
    lblCredit = Val(lblCredit) + 1
    iCnt = iCnt + 1
Else
    iCnt = 1
    Timer1.Enabled = False
    mWinInProgress = False
   miCounter = miCounter + 1
Debug.Print miCounter
 
End If
End Sub

Open in new window

how can this be done ?
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Timers run asynchronously and the timer event will fire each time the interval is reached. The loop you are performing is resetting the timer interval and is most likely causing the timer to stop and restart each time through your loop. Once the loop finishes, your Start function ends and ends your code. If all you want to do is have the timer fire 12 times, drop the loop and set mlAmount to 12. The final thing would be to put a while loop at the end of your Start method that checks if the Timer is enabled, an if so, do the doevents.
Try this.

Option Explicit

 Dim mlAmount As Long
 Dim iCnt As Long
 Dim miCounter As Integer
 Dim i As Integer

Sub Start()
    mlAmount = 10
    DoEvents
    Timer1.Interval = 5 '60
    Timer1.Enabled = True
End Sub

Private Sub Command1_Click()
Start
End Sub

Private Sub Timer1_Timer()

    For i = 1 To 12
    iCnt = 1
    
        If iCnt <= mlAmount Then
            lblWinThisRun.Caption = iCnt
            lblCredit = Val(lblCredit) + 1
            iCnt = iCnt + 1
        Else
            iCnt = 1
            Timer1.Enabled = False
            mWinInProgress = False
            miCounter = miCounter + 1
            Debug.Print miCounter
            DoEvents
        End If
Next
 
End Sub

Open in new window

What do you want to happen?  Currently that loop is setting the timer interval, and turning it on. The other 11 times it sets interval and enables effectively does nothing.



Option Explicit
 Dim mlAmount As Long
 Dim iCnt As Long
 Dim miCounter As Integer


Sub Start()
    iCnt = 1
    mlAmount = 10
    DoEvents
    Timer1.Interval = 60
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
If iCnt <= mlAmount Then
    lblWinThisRun.Caption = iCnt
    lblCredit = Val(lblCredit) + 1
    iCnt = iCnt + 1
Else
    iCnt = 1
    Timer1.Enabled = False
    mWinInProgress = False
   miCounter = miCounter + 1
Debug.Print miCounter
 
End If
End Sub

Open in new window

Keep in mind that timer1.interval = 60 is 60ms, not 1minute.
ASKER CERTIFIED SOLUTION
Avatar of ktaczala
ktaczala
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
Not sure I undersood correctly but try this
Private counter As Long

Private Sub Command1_Click()
   counter = 12
   Me.Caption = "Seconds remain: " & counter
   Timer1.Interval = 1000
   Timer1.Enabled = True
End Sub

Private Sub Form_Load()
   Caption = "Press button to start"
End Sub

Private Sub Timer1_Timer()
   counter = counter - 1
   If counter < 0 Then
      Timer1.Enabled = False
      Me.Caption = "Game over!"
      Exit Sub
   End If
   Me.Caption = "Seconds remain: " & counter
End Sub

Open in new window

Avatar of isnoend2001

ASKER

Looks like i need to give more clarification.
This is an 80 number keno game where 20 spots are randomly selected each time the start button is clicked.
During the randomize if a win then the 2 labels are visually rotated the amount of the win along with a sound.
This works fine as long as the start button is clicked to start each run
here is where the problem arises when the randomize runs in a loop
There is an option where the program will run continually without clicking the start button before each randomize.
Then another where 12 runs will automatically run

lblWinThisRun.Caption = iCnt
        lblCredit = Val(lblCredit)
the mlAmount = 10 'could be any amount i put 10 as an example
Why not just use
For i=1 To 12
   cmdRun_Click
Next i

Open in new window

which version of vb?

vb.net is  btnstart.performclick
vb6
Thanks Ark: Re:
For i=1 To 12
   cmdRun_Click
Next i
 Seems I had tried that. I don't remember.
There is no start button on the 12 runs.
Why:
The  main form   when ran can produce a bonus  Where 12 free runs are produced and ran automatically. If the main form is in autoplay mode the results of the bonus are visual rotating
2 labels
1. the amount of money the user has on the machine
2. the amount of the win
I try to make the game run just like at the Casinos wher the 2 labels visually rotate the labels and play a sound as they are rotated
Your clarification only complicated the question. You're talking about elements which aren't here, and aren't part of the original question.  

What do you want to happen?

Click a button,
Something happens 12times, once every (1 minute?)
i think i have given too much info and only complicated the question. I have attempted many of the fixes suggested, but have not been successful. so i made a little sample program.
I want to run the timer 12 different times with each time rotating the labels with the mlAmount
the problem with this code is it only runs the timer 1 time and after that calls to the timer are ignored

 
    Private Sub cmdStart_Click()
 Start
End Sub
Sub Start()
    Dim i As Integer
    imRuns = 12
    For i = 1 To imRuns ' run the timer 12 times in this case, each time rotating the labels with mlAmount
      iCnt = 1
    mlAmount = 10 'sample could be any amount
    DoEvents
    Timer1.Interval = 60
    Timer1.Enabled = True
    Next
End Sub
Private Sub Timer1_Timer()
'the labels will rotate the number of times in mlAmount

If iCnt <= mlAmount Then
    lblWinThisRun.Caption = iCnt ' always starts at 0
    lblCredit = Val(lblCredit) + 1 'add 1 for each
    iCnt = iCnt + 1
Else
    iCnt = 1
    Timer1.Enabled = False
    mWinInProgress = False
   miCounter = miCounter + 1
Debug.Print miCounter
End If
End Sub

Open in new window

Is it only running once, or is the timer firing so fast that you are not seeing the changes in your label's caption? When it is done, what text is appearing in the label?

Again, the loop is not necessary, as once you enable a timer, it will continue until you disable it.

As someone mentioned above, Interval is in milliseconds, so an interval of 60 is 60 one-thousandths of a second.
I don't have VB6, so I tweaked the code to work in visual studio 2008.

the code I pasted above works just fine.
 I had to add a timer1.stop after the next loop finished otherwise it goes on forever.
the snapshot below is what I got for a result
ScreenCapture.jpg
I put a break here: If iCnt <= mlAmount Then
The timer is only firing once. the first time the timer is called it works,
and the timer  Timer1.Enabled = False is hit
but after that  Timer1.Enabled = True does not fire the timer
What happens when you slow your timer down to 10 seconds (Interval = 10000)? It sounds like your timer is running but it is occurring so fast that you are not observing what is happening when you attempt to debug.

An alternative to using the timer would be to do all of your code inside your For loop and add a Sleep call.
That's because you sub start finishes before the timer even ticks once.
you need the wait loop.
oops sorry , I meant before timer ever triggers.  (60 ms)
thanks that code seems to work
btw this does not work in vb 6
Timer1.Stop() '<<<< stop the timer until your processing is done
this does
Timer1.enabled = false