Timers

Hi!

Is there any way i can know how many times a timer has ticked

Also, if i make a call to timer.Start(), the timer starts and my code continues execution. Is there any way I can block the code from furthur execution till the timer is done with its work

Thanks



Answer this question

Timers

  • testie

    In the first part of your question: Are you talking about how many times the timer has fired  If so, you could just increment a simple integer value

    Dim intTimes As Integer

    in your Timer_Tick event:

    intTimes +=1

    ------------------------------

    Part 2:

    You could start a thread and pause your first or create a loop from a simple boolean, which loops until the boolean = true for example.

    do while blnSomething = False

    Loop

    -----------------------------------

    Thread.Sleep(interval here) for main code

    Call timer here

  • lordmarc

    It sounds like you want to run a loop at a specified interval for a given duration.  I don't think you want a timer...  Try something like:


    'Declare some classwide variables

    'Create a DateTime to hold the last time we did our work
    Dim LastRunTime As New DateTime

    'Create an TimeSpan to hold the amount of time that must pass between loops
    Dim Interval As TimeSpan

    'Create an Integer to hold the number of times we want to do our work
    Dim Reps As Integer



    'Create the subroutine which will do your work
    Private Sub DoMyWork()

    'Create a loop to repeat your work
            For i As Integer = 1 To Reps

    'Check to see if enough time has passed 
                If Now.Subtract(LastRunTime).TotalMilliseconds >= Interval.TotalMilliseconds Then

    'If so, set the last run time to now
                    LastRunTime = Now

    'Add the rest of your code to actuall do your work
    ' ...DO WORK...

    'If enough time has not yet passed
                Else

    'Set the value of i back 1 so that the loop continues until its time to do more work
                    i -= 1

                End If

            Next

    End Sub



    'Execute your Work from a Button.Click Event
    Private Sub Button1_Click(ByVal sender as Object, e as System.EventArgs) Handles Button1.CLick()

    'Set the number of times to do the work; this would be the Tick Count you were looking for; this example will run a dozen times
            Reps = 12

    'Set the minimum time between executions; this would be the Interval of your Timer; this example runs every 3 seconds
            Interval = New TimeSpan(0, 0, 3)
            
    'Set the value of Last Run Time; if you set it to Now() then the first execution of work won't occur until Interval has elapsed for the first time; set the value to Now() - Interval to cause work to execute immediately; this example begins execution immediately
    LastRunTime = Now.Subtract(Interval)

    'Call your work sub
    DoMyWork()

    End Sub



    A design like this will allow you to avoid messing threading issues.  Hope this helps!

  • Timers