Solved

calling a timer in a loop

Posted on 2014-01-30
21
224 Views
Last Modified: 2014-01-31
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 ?
0
Comment
Question by:isnoend2001
  • 6
  • 6
  • 3
  • +3
21 Comments
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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

0
 
LVL 13

Expert Comment

by:MrBullwinkle
Comment Utility
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

0
 
LVL 13

Expert Comment

by:MrBullwinkle
Comment Utility
Keep in mind that timer1.interval = 60 is 60ms, not 1minute.
0
 
LVL 12

Accepted Solution

by:
ktaczala earned 500 total points
Comment Utility
Option Explicit
 Dim mlAmount As Long
 Dim iCnt As Long
 Dim miCounter As Integer
 Dim waitflag as Boolean '<<<< declare this


Sub Start()
    Dim i As Integer
    For i = 1 To 12
    waitflag = True '<<<< set wait flag to true
      iCnt = 1
    mlAmount = 10
    ' DoEvents()   '<<<< rem this out
    Timer1.Interval = 60
    Timer1.Enabled = True
    Do While waitflag = True
    Application.DoEvents()
    loop
    Next
End Sub

Private Sub Timer1_Timer()

If iCnt <= mlAmount Then
    lblWinThisRun.Caption = iCnt
    lblCredit = Val(lblCredit) + 1
    iCnt = iCnt + 1
Else
 Timer1.Stop() '<<<< stop the timer until your processing is done
   iCnt = 1
    ' Timer1.Enabled = False '<<<Remove
    mWinInProgress = False
   miCounter = miCounter + 1
Debug.Print miCounter
waitflag = False ' <<<< set wait flag to false

End If
End Sub


First time through the timer will start and then the sub will wait (loop)

when timer = 60 then stop the timer
process your work
set waitflag to false
do while will now fall through

I assume you only want the timer to stop when the "else" path is taken.
timer1.stop() is the same as timer1.enabled = False  ' personal preference
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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

0
 

Author Comment

by:isnoend2001
Comment Utility
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
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
Why not just use
For i=1 To 12
   cmdRun_Click
Next i

Open in new window

0
 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
which version of vb?

vb.net is  btnstart.performclick
0
 

Author Comment

by:isnoend2001
Comment Utility
vb6
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:isnoend2001
Comment Utility
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
0
 
LVL 13

Expert Comment

by:MrBullwinkle
Comment Utility
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?)
0
 

Author Comment

by:isnoend2001
Comment Utility
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

0
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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.
0
 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
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
0
 

Author Comment

by:isnoend2001
Comment Utility
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
0
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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.
0
 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
That's because you sub start finishes before the timer even ticks once.
you need the wait loop.
0
 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
oops sorry , I meant before timer ever triggers.  (60 ms)
0
 

Author Closing Comment

by:isnoend2001
Comment Utility
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
0
 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
Ok
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now