VB中Timer的间隔最大时间是多少?
VB中Timer的间隔最大为65535ms。
Timer控件是一个定时发生某个动作的控件,它有一个重要属性Interval,这个值设定每个多长时间执行一次动作,单位是毫秒,还有一个重要事件是Timer,双击Timer控件即可自动产生这个事件的框架,在这个框架里面写入每隔Interval毫秒就要执行的代码动作。
可以通过编程实现长时间:
Private Sub Form_Load()
Timer1.Interval = 60000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Static s_Minutes As Long
s_Minutes = s_Minutes + 1
If s_Minutes = 60 Then
s_Minutes = 0
Debug.Print "一小时到了"
End If End Sub
但是可以通过编程实现长时间
Private Sub Form_Load()
Timer1.Interval = 60000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Static s_Minutes As Long
s_Minutes = s_Minutes + 1
If s_Minutes = 60 Then
s_Minutes = 0
Debug.Print "一小时到了"
End If
End Sub
或者代用API实现长达 49 天以上
'模块中代码
Dim lTimerId As Long
Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Private Sub TimerProc(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lTimerId As Long, ByVal lTime As Long)
Dim lResult As Long
lResult = StopTimer(lTimerId)
Call InsertYourProcessNameHere
'code to be executed after interval
End Sub
Public Sub StartTimer(lInterval As Long) 'convert interval to milliseconds prior to passing
lTimerId = SetTimer(0, 0, lInterval, AddressOf TimerProc)
End Sub
Public Function StopTimer(lTimerId As Long) As Long
'must pass the TimerId returned by SetTimer
StopTimer = KillTimer(0, lTimerId)
End Function
'调用方式
Call StartTimer(5000) '5 seconds