Private Sub btnFormsTimer_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnFormsTimer.Click
If timerRunning = True Then
timerRunning = False
avgTimerTicks = avgTimerTicks / numTimerExpirations
lFormsTimer.Text = "Avg. Timer Interval: " + avgTimerTicks.ToString() + _
" ms (" + numTimerExpirations.ToString() + ")"
btnFormsTimer.Text = "Start Forms Timer"
btnThreadingTimer.Enabled = True
Else
timerRunning = True
lFormsTimer.Text = "Timer running"
btnFormsTimer.Text = "Stop Forms Timer"
btnThreadingTimer.Enabled = False
avgTimerTicks = 0
numTimerExpirations = 0
prevTimerTicks = Environment.TickCount
formsTimer.Interval = 100 ' 利用窗体上放置的Timer控件
formsTimer.Enabled = True
End If
End Sub
Private Sub btnProcessing_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnProcessing.Click
btnProcessing.Enabled = False
Threading.Thread.CurrentThread.Priority = Threading.ThreadPriority.BelowNormal
For i As Long = 0 To 5000000
Next
btnProcessing.Enabled = True
Threading.Thread.CurrentThread.Priority = Threading.ThreadPriority.Normal
End Sub
Private Sub ThreadingTimerTick(ByVal state As Object)
If timerRunning = False Then
threadingTimer.Dispose()
threadingTimer = Nothing
Else
Dim currentTimerTicks As Long = Environment.TickCount
avgTimerTicks = avgTimerTicks + (currentTimerTicks - prevTimerTicks)
numTimerExpirations = numTimerExpirations + 1
prevTimerTicks = currentTimerTicks
End If
End Sub