类模块程序
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
Form1.Print Now()
DoEvents
End Sub
窗体程序
Public ID1
Dim bz1 As Boolean
Private Sub Form_Load()
bz1 = False
Command1.Caption = "定时开始"
End Sub
Private Sub Command1_Click()
If bz1 = False Then
'每50ms调用一次函数
ID1 = SetTimer(0, 0, 50, AddressOf TimerProc)
bz1 = True
Command1.Caption = "定时结束"
Else
KillTimer 0, ID1
bz1 = False
Command1.Caption = "定时开始"
End If
End Sub