如何不断检查系统时间的变化?

时间:2016-10-30 15:10:30

标签: vb.net

我正在尝试制作一个每小时发出声音的节目,我该怎么做?

我想也许不断检查系统时间,如果它等于“1:00 2:00 3:00等......”然后发出声音,但我不知道该怎么做,一些帮助将不胜感激:))

2 个答案:

答案 0 :(得分:0)

添加一个Timer,设置Interval为1000(1秒)并在Tick事件中检查当前时间

   Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'check time here and put it in Label1
        Label1.Text = TimeOfDay.ToString("hh:mm:ss")

        'if currenttime = alarmtime then do something
        If TimeOfDay.ToString("hh:mm:ss") = "10:01:00" Then Beep()
    End Sub

有关如何阅读当前时间的更多信息: https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateandtime(v=vs.110).aspx

答案 1 :(得分:0)

创建一个计时器,将间隔设置为500毫秒(.5s),设置为1秒可能导致错过刻度线。这将触发tick事件,检查当前时间分钟和秒是否等于零,然后触发。在定时器在第二秒内再次检查的情况下,定时器在声音发射之间被禁用(因为定时器被称为半秒钟)。

ReadOnly _tmr As New Timer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    _tmr.Interval = 500
    AddHandler _tmr.Tick, AddressOf tmr_Tick
    _tmr.Enabled = True
End Sub

Private Sub tmr_Tick(sender As Object, e As EventArgs)
    Dim curDateTime As DateTime = Now

    If curDateTime.Minute = 0 And curDateTime.Second = 0 Then
        _tmr.Enabled = False
        PlaySound()
        _tmr.Enabled = True
    End If

End Sub

Private Sub PlaySound()
    '// Play Sound
End Sub