By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,423 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

Background Timer

P: 116

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.Timer can run slow due to the time taken to run code each tick....?

Oct 26 '13 #1
Share this Question
Share on Google+
2 Replies

P: 296
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...

Oct 27 '13 #2

P: 116
Thanks Des,

I decided in the end that a backgroundWorker 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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub countBackground_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles countBackground.DoWork
  2.         Dim progresscount As Integer = 0
  3.         Do
  4.             progresscount += 1
  5.             countBackground.ReportProgress(progresscount)
  6.             Threading.Thread.Sleep(1000)
  7.             If countBackground.CancellationPending = True Then Exit Do
  8.         Loop
  9. End Sub
  11. Private Sub countBackground_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles countBackground.RunWorkerCompleted
  12.         mycount= 0
  13. End Sub
  15. Private Sub countBackground_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles countBackground.ProgressChanged
  16.         mycount= mycount+ 1
  17.         mainCount.Text = mycount.ToString
  18. End Sub
All the best.
Oct 29 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.