在VB中如何实现定时触发某段程序的问题???

jinhailv 2005-04-04 03:44:52
我想在VB中定时的执行某段程序:即每隔一小时或每隔十分钟调用某个函数,而在VB中利用控件Timer它的时间间隔最多为65秒。请各位大哥帮忙看看,有什么方法可以实现时间间隔更长这一功能;多谢了!!
...全文
326 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kofchris 2005-04-04
  • 打赏
  • 举报
回复
最简单的,用timeGetTime
eg:

dim time as long
do
time=timeGetTime
DoEvents
Loop While timeGetTime-time < 具体间隔时间
下面为你要实现的功能代码......
of123 2005-04-04
  • 打赏
  • 举报
回复
'用 API 做长延时的 Timer, 时间间隔(毫秒)为 Long 型,可达 47 天。
'模块中代码
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
zyg0 2005-04-04
  • 打赏
  • 举报
回复
Private Sub Form_Unload(Cancel As Integer)


KillTimer Me.hwnd, 0
' 卸载所时钟

End Sub
zyg0 2005-04-04
  • 打赏
  • 举报
回复

Private Sub Form_Load()
'1秒执行一次
SetTimer Me.hwnd, 0, 1000, AddressOf TimerProc
end sub
------------------模块中
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Dim Tel As Long'记录时间
'这里写 执行代码
Public Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)

If Tel = 30 Then'30秒执行一次,可以改这个数字到任何大小,long类型


Tel = 0
End If
Tel = Tel + 1

End Sub
viena 2005-04-04
  • 打赏
  • 举报
回复
判断时间准确一些,但前提是用户没有修改系统时间
viena 2005-04-04
  • 打赏
  • 举报
回复
Dim date1 As date

Private Sub Form_Load()
date1=now
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
dim dateTmp as date
dateTmp = now
If DateDiff("n", date1, dateTmp)=10 Then '每隔十分钟,如果小时"n"改为"h"
Call YourOtherSub
date1 = dateTmp
End If
End Sub
chendjin 2005-04-04
  • 打赏
  • 举报
回复
Dim nTimeM As Long

Private Sub Form_Load()
nTimeM = 0
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
nTimeM = nTimeM + 1
If nTimeM >= 60*60 Then '每隔一小时
Call YourOtherSub
nTimeM =0
End If
End Sub

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧