Public Sub WaitFiveSeconds()
If TimeOfDay >= #11:59:55 PM# Then
MsgBox("The current time is within 5 seconds of midnight" & _
vbCrLf & "The timer returns to 0.0 at midnight")
Return
End If
Dim Start, Finish, TotalTime As Double
If (MsgBox("Press Yes to pause for 5 seconds", MsgBoxStyle.YesNo)) = _
MsgBoxResult.Yes Then
Start = Microsoft.VisualBasic.DateAndTime.Timer
Finish = Start + 5.0 ' Set end time for 5-second duration.
Do While Microsoft.VisualBasic.DateAndTime.Timer < Finish
' Do other processing while waiting for 5 seconds to elapse.
Loop
TotalTime = Microsoft.VisualBasic.DateAndTime.Timer - Start
MsgBox "Paused for " & TotalTime & " seconds"
End If
End Sub
请注意,由于 Timer 也是 System.Threading、System.Timers 和 System.Windows.Forms 命名空间中的定义类,因此必须用 Microsoft.VisualBasic 命名空间限定 Timer 属性。
Timer在vb.net中也有
[Visual Basic]
Public Class Timer2
Public Shared Sub Main()
' Create a new Timer with Interval set to 10 seconds.
Dim aTimer As New System.Timers.Timer(10000)
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Only raise the event the first time Interval elapses.
aTimer.AutoReset = False
aTimer.Enabled = True
Console.WriteLine("Press 'q' to quit the sample.")
While Console.Read() <> CInt("q")
End While
End Sub
' Specify what you want to happen when the event is raised.
Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class