计时器

WPF计时器
在.Net中有不少计时器,包括:

#1.System.Threading.Timer

2.System.Timers.Timer
3.System.Windows.Forms.Timer
4.System.Web.UI.Timer
5.System.Windows.Threading.DispatcherTimer

其中第四个主要用于web开发中,第一个和第二个的触发事件和UI处于不同的线程,因此如果使用它们的触发事件来改变UI,会发生对象被占用的异常,第三个是WinForm的计时器,在WPF中也可以使用,不过必须添加System.Windows.Forms的引用,第五个是WPF自己的计时器,一般在WPF程序中最好使用该计时器,使用例子如下:

Class MainWindow
    '新定义一个计时器
    Dim aTimer As New System.Windows.Threading.DispatcherTimer
    Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
        '计时器周期性触发事件。TimerEvent是触发的事件
        AddHandler aTimer.Tick, AddressOf TimerEvent
        '设置计时器触发周期,2秒
        aTimer.Interval = New TimeSpan(0, 0, 2)
        '启用计时器
        aTimer.IsEnabled = True
    End Sub
    '计时器周期性触发的事件TimerEvent
    Sub TimerEvent()
        label.Content += DateTime.Now.Second & vbCrLf
    End Sub  
End Class 

你可能感兴趣的:(#,GUI控件,vb.net)