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.ti mer or system.windows. forms.timer. I read the system.timer.ti mer 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:
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.ti mer or system.windows. forms.timer. I read the system.timer.ti mer 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:
Code:
Dim I = 0
Dim Running As Boolean = False
Private t As New System.Timers.Timer(1000)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load#
AddHandler t.Elapsed, AddressOf TimerDone
t.Interval = 1000
t.SynchronizingObject = Me
End Sub
Public Sub TimerDone(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
I = I - 1
Label1.Text = I
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Running = False Then
Label1.Text = TrackBar1.Value
Label2.Text = ""
Button1.Text = "Stop"
Timer2.Interval = Label1.Text * 1000
I = (Timer2.Interval / 1000) - 1
t.Enabled = True
Timer2.Enabled = True
Running = True
Else
Timer2.Enabled = False
t.Enabled = False
TrackBar1.Enabled = True
Button1.Text = "Start"
Label2.Text = "Stopped"
Running = False
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Timer2.Enabled = False
t.Enabled = False
Button1.Text = "Start"
I = 0
Label2.Text = "Done"
TrackBar1.Enabled = True
Button1.Text = "Start"
Running = False
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Label1.Text = TrackBar1.Value
End Sub
End Class