VB怎么让语句间隔一段时间执行?
我用VB的API中的SLEEP函数,设置语句中间间隔一定的时间,但运行的时候好像不是每条语句都间隔执行,而是把所有的SLEEP整合到一起,然后在执行语句。就是说,本来有5...
我用VB的API中的SLEEP函数,设置语句中间间隔一定的时间,但运行的时候好像不是每条语句都间隔执行,而是把所有的SLEEP整合到一起,然后在执行语句。就是说,本来有5个SLEEP分开的,但是程序运行的时候却是开始的时候间隔10秒然后把语句一起执行。
用什么方法才能让语句间隔一定时间执行呢?其他方法也可以。
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Activate()
Shell "notepad.exe", vbNormalFocus
Sleep 2000
SendKeys "test"
Sleep 2000
SendKeys "%"
Sleep 2000
SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}~"
Sleep 2000
SendKeys "d:\test.txt"
Sleep 2000
SendKeys "~"
End Sub 展开
用什么方法才能让语句间隔一定时间执行呢?其他方法也可以。
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Activate()
Shell "notepad.exe", vbNormalFocus
Sleep 2000
SendKeys "test"
Sleep 2000
SendKeys "%"
Sleep 2000
SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}~"
Sleep 2000
SendKeys "d:\test.txt"
Sleep 2000
SendKeys "~"
End Sub 展开
5个回答
展开全部
在vb程序中让两条语句的执行间隔一段时间,可以用API函数的Sleep函数实现。
参考例子:
'************************************************************
'延时
Public Sub Wait(delay As Single)
'Delay app the selected no. of seconds
'example Wait 3.5
'app will loop here for 3.5 seconds
starttime! = Timer
Do Until Timer >= starttime! + delay
Loop
End Sub
'************************************************************
延时2秒
则用
wait(2)
参考例子:
'************************************************************
'延时
Public Sub Wait(delay As Single)
'Delay app the selected no. of seconds
'example Wait 3.5
'app will loop here for 3.5 seconds
starttime! = Timer
Do Until Timer >= starttime! + delay
Loop
End Sub
'************************************************************
延时2秒
则用
wait(2)
展开全部
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Activate()
Shell "notepad.exe", vbNormalFocus
Sleep 2000
SendKeys "test"
DoEvents
Sleep 2000
SendKeys "%"
DoEvents
Sleep 2000
SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}~"
DoEvents
Sleep 2000
SendKeys "d:\test.txt"
DoEvents
Sleep 2000
DoEvents
SendKeys "~"
End Sub
用sleep会将程序挂起 用doevents转让控制权 就好用了
Private Sub Form_Activate()
Shell "notepad.exe", vbNormalFocus
Sleep 2000
SendKeys "test"
DoEvents
Sleep 2000
SendKeys "%"
DoEvents
Sleep 2000
SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}~"
DoEvents
Sleep 2000
SendKeys "d:\test.txt"
DoEvents
Sleep 2000
DoEvents
SendKeys "~"
End Sub
用sleep会将程序挂起 用doevents转让控制权 就好用了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Option Explicit
Private Sub Form_Activate()
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Timer1.Interval = 2000
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Static sum As Long
sum = sum + 1
If sum = 1 Then
Shell "notepad.exe", vbNormalFocus
ElseIf sum = 2 Then
SendKeys "test"
ElseIf sum = 3 Then
SendKeys "%"
ElseIf sum = 4 Then
SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}~"
ElseIf sum = 5 Then
SendKeys "d:\test.txt"
ElseIf sum = 6 Then
SendKeys "~"
Timer1.Enabled = False
End If
End Sub
Private Sub Form_Activate()
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Timer1.Interval = 2000
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Static sum As Long
sum = sum + 1
If sum = 1 Then
Shell "notepad.exe", vbNormalFocus
ElseIf sum = 2 Then
SendKeys "test"
ElseIf sum = 3 Then
SendKeys "%"
ElseIf sum = 4 Then
SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}~"
ElseIf sum = 5 Then
SendKeys "d:\test.txt"
ElseIf sum = 6 Then
SendKeys "~"
Timer1.Enabled = False
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Activate()
Shell "notepad.exe", vbNormalFocus
DoEvents
Sleep 1000
SendKeys "test"
DoEvents
Sleep 1000
SendKeys "%"
DoEvents
Sleep 1000
SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}~"
DoEvents
Sleep 1000
SendKeys "d:\test.txt"
Sleep 1000
SendKeys "~"
End Sub
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Activate()
Shell "notepad.exe", vbNormalFocus
DoEvents
Sleep 1000
SendKeys "test"
DoEvents
Sleep 1000
SendKeys "%"
DoEvents
Sleep 1000
SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}~"
DoEvents
Sleep 1000
SendKeys "d:\test.txt"
Sleep 1000
SendKeys "~"
End Sub
参考资料: http://baike.baidu.com/view/327466.htm?fr=ala0_1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没这么搞过,实在不行,就给整空循环!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询