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

vb .net timer problem with Windows 7 dual processors

P: 1
I have been writing visual basic and vb .net applications for years and have come across this problem with the timer component under Windows 7 32bit on dual core Intel processors. This problem only occurs with Windows 7 running on a dual core processor, single core and under the virtual pc on any processor works.

I have attached a test program to show this problem. The program uses 2 timers to count down to 0. It does not matter what type of timer component I use system.timer.timer or system.windows.forms.timer. I read the system.timer.timer may fix this problem so I changed the program to use it but it did the exact same thing. Left it in anyway.

The program: Timer1 is set to a number of ms for a given amount of time say 150000 for 150 seconds and started. The timer will expire correctly. At the same time a second timer Timer2 is set to 1000 ms for a count down display to 0. Timer2 fires and the count is updated. This happens and everything looks fine. When Timer1 fires I disable timer2 so it will stop counting down. Timer2's countdown is at 2 instead of 0. It is firing slow. If I increase the value of timer1 to say 500 seconds timer2 is behind by 6 seconds. These amounts are the same for the 2 very different computers I ran this on but both are dual processor systems running Windows 7. The exact code works correctly on Windows Xp on the same processors and works under Windows 7 on a Pentium 4 system.

Any ideas?

Code:

Expand|Select|Wrap|Line Numbers
  1.     Dim I = 0
  2.     Dim Running As Boolean = False
  3.  
  4.     Private t As New System.Timers.Timer(1000)
  5.  
  6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load#
  7.         AddHandler t.Elapsed, AddressOf TimerDone
  8.         t.Interval = 1000
  9.         t.SynchronizingObject = Me
  10.  
  11.     End Sub
  12.  
  13.  
  14.     Public Sub TimerDone(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
  15.         I = I - 1
  16.         Label1.Text = I
  17.  
  18.     End Sub
  19.  
  20.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  21.         If Running = False Then
  22.             Label1.Text = TrackBar1.Value
  23.             Label2.Text = ""
  24.             Button1.Text = "Stop"
  25.             Timer2.Interval = Label1.Text * 1000
  26.             I = (Timer2.Interval / 1000) - 1
  27.             t.Enabled = True
  28.             Timer2.Enabled = True
  29.             Running = True
  30.         Else
  31.  
  32.             Timer2.Enabled = False
  33.             t.Enabled = False
  34.             TrackBar1.Enabled = True
  35.             Button1.Text = "Start"
  36.             Label2.Text = "Stopped"
  37.             Running = False
  38.         End If
  39.  
  40.     End Sub
  41.  
  42.  
  43.     Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
  44.         Timer2.Enabled = False
  45.         t.Enabled = False
  46.         Button1.Text = "Start"
  47.         I = 0
  48.         Label2.Text = "Done"
  49.         TrackBar1.Enabled = True
  50.         Button1.Text = "Start"
  51.         Running = False
  52.  
  53.     End Sub
  54.  
  55.     Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
  56.         Label1.Text = TrackBar1.Value
  57.  
  58.     End Sub
  59.  
  60. End Class
Attached Files
File Type: zip TimerTest.zip (87.6 KB, 36 views)
Apr 8 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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