Background Timer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robertybob
    New Member
    • Feb 2013
    • 116

    Background Timer

    Hi.

    I realise there are 3 different timers in VB.Net but I'm not certain which I should be using for the following. Could anyone help with some basic code and type of timer I should use?

    This is the scenario simplified for clarity.

    Main form runs all kinds of processes etc and a timer needs to run every second that increments a label value constantly - ie label text starts at 0 and displays the seconds elapsed in real time.

    I'd normally just use the Systems.Windows .Form.Timer tick to change the label text but does this get interrupted by anything being done on the form? Also I believe a System.Timers.T imer can run slow due to the time taken to run code each tick....?

    Thanks!
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #2
    Try instantiating the timer manually and on a new thread. Updating the display can be done a number of ways. If you want to update the display once per second to show the time, and being off by nearly 1 full second is not a problem, then update your label once a second. If it's important your label be very accurate then you need to sample the clock much more often and then update the label when it changes.
    For example: If you update the label once per second you can be .999 of a second off because it's possible the time actually changes .001 second after you update the label. That means your display is off by about a second.
    If you want more accuracy it's really a bad idea to update the label every .001 second because updating a UI control uses a lot of resources and takes time. Instead have a background loop checking the seconds and only when the actual time changes then update the label. Again - you have to decide if accuracy is that important - and if it is, how much. Checking the time every .001 second is really a bit much but...

    Des

    Comment

    • robertybob
      New Member
      • Feb 2013
      • 116

      #3
      Thanks Des,

      I decided in the end that a backgroundWorke r would suffice in this case as it's just a count in the background and doesn't have to be exactly accurate, just generally accurate.

      Still means I'm not totally up to speed on the uses for the different timers but the following seems to do the trick.

      Code:
      Private Sub countBackground_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles countBackground.DoWork
              Dim progresscount As Integer = 0
              Do
                  progresscount += 1
                  countBackground.ReportProgress(progresscount)
                  Threading.Thread.Sleep(1000)
                  If countBackground.CancellationPending = True Then Exit Do
              Loop
      End Sub
      
      Private Sub countBackground_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles countBackground.RunWorkerCompleted
              mycount= 0
      End Sub
      
      Private Sub countBackground_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles countBackground.ProgressChanged
              mycount= mycount+ 1
              mainCount.Text = mycount.ToString
      End Sub
      All the best.

      Comment

      Working...