I was under the impression that System.Timers.Timer creates its own thread and that Microsoft recommends this type of timer to do tasks that do more accurate timing (as opposed to Windows.Forms.Timer, which runs in the UI thread).
The code below (I think) should be copy-and-pasteable into a project with an empty form. On my machine, I cannot get tmrWork to tick any faster than about 60 times per second, and it's amazingly unstable.
Public Class Form1
Private lblRate As New Windows.Forms.Label
Private WithEvents tmrUI As New Windows.Forms.Timer
Private WithEvents tmrWork As New System.Timers.Timer
Public Sub New()
Me.Controls.Add(lblRate)
InitializeComponent()
End Sub
Private StartTime As DateTime = DateTime.Now
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
Handles MyBase.Load
tmrUI.Interval = 100
tmrUI.Enabled = True
tmrWork.Interval = 1
tmrWork.Enabled = True
End Sub
Private Counter As Integer = 0
Private Sub tmrUI_Tick(sender As Object, e As System.EventArgs) _
Handles tmrUI.Tick
Dim Secs As Integer = (DateTime.Now - StartTime).TotalSeconds
If Secs > 0 Then lblRate.Text = (Counter / Secs).ToString("#,##0.0")
End Sub
Private Sub tmrWork_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) _
Handles tmrWork.Elapsed
Counter += 1
End Sub
End Class
In this particular simple case, putting everything in tmrUI will yield the same performance. I guess I never tried to get a System.Timers.Timer to go too fast, but this performance looks just too bad to me. I wrote my own class to use the high performance timer in hardware but it seems like there should be a built-in timer that can do, say 100 ticks per second?
What's going on here?