help using timer

vb6 Nepal
  • 13 years ago

    i want to loop a task using timer

    but how can i count the numer of repetitions in timer

  • 13 years ago
    Use a Global variable, which you declare in your Module.

    For instance, you call the tally intTally and declare it as such in the module:

    Global intTally As Integer

    Then you add the following to your timer's sub:

    Private Sub Timer1_Timer()
    ' do stuff here
    intTally = intTally + 1
    End Sub












  • 13 years ago
    Dim cntr as Integer

    Do while condition
        cntr=cntr + 1
        Timer1.Enabled=False
    Loop


    Hope it can add you more idea...


    jireh('',)












  • 13 years ago

    Salam brothers and friedns ....

    To work this code need to have one Timer Component (with name:  Timer1) and one Label (with name: Label1)

    VB text is writen down

    Private Sub Form_Load()
    Label1.Caption = 0
    Timer1.Interval = 1000
    End Sub


    Private Sub Timer1_Timer()
    If Label1.Caption = 20 Then Me.Timer1.Enabled = False
    Me.Label1.Caption = Label1.Caption + 1
    End Sub


     

  • 13 years ago
    Depends on what you need to count? You musn't use no counter, because since timer has its Interval property, which means count of milliseconds between Timer_Tick events. So enough for you should be recording Date.Now while starting timer, and when you want to know 99% right number of Tick till Now, you simply do "ActualLoopCount% = Now.Subtract(StartTime).TotalMilliseconds / TimerInterval"
    But if you need that 100% sure you should use cntr% = cntr% + 1 in every Tick...

    I also advice you to not use label for counting timer lapses, use that global variable or use class for it as whole. Timer needs optimal code to run properly, and storing data in form controls is very big late. If timer had interval = 1000 &>  it should not be visible  but if you need interval  around  100ms use base data types to count it.




Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The question of whether computers can think is just like the question of whether submarines can swim.” - Edsger W. Dijkstra