VB延时函数出错,代码很简单的,请帮忙看看
''''测试延时函数'''''Print"开始,5秒后第二行"Print"开始,5秒后第二行"Print"开始,5秒后第二行"delay5Print"开始,5秒后第二行"...
''''测试延时函数'''''
Print "开始,5秒后第二行"
Print "开始,5秒后第二行"
Print "开始,5秒后第二行"
delay 5
Print "开始,5秒后第二行"
Print "开始,5秒后第二行"
'''''测试结束''''''''
按理应该先打出3行,5秒后 另外2行一起打出,谁知道他先打出2行,5秒后一下打出3行,这是怎么回事啊,真是灵异事件啊,请各位帮忙看看,谢谢。
Static Sub delay(delaytime)
Const secondsinday = 24& * 60& * 60&
loopfinish = Timer + delaytime
If loopfinish > secondsinday Then
loopfinish = loopfinish - secondsinday
Do While Timer > loopfinish
Loop
End If
Do While Timer < loopfinish 展开
Print "开始,5秒后第二行"
Print "开始,5秒后第二行"
Print "开始,5秒后第二行"
delay 5
Print "开始,5秒后第二行"
Print "开始,5秒后第二行"
'''''测试结束''''''''
按理应该先打出3行,5秒后 另外2行一起打出,谁知道他先打出2行,5秒后一下打出3行,这是怎么回事啊,真是灵异事件啊,请各位帮忙看看,谢谢。
Static Sub delay(delaytime)
Const secondsinday = 24& * 60& * 60&
loopfinish = Timer + delaytime
If loopfinish > secondsinday Then
loopfinish = loopfinish - secondsinday
Do While Timer > loopfinish
Loop
End If
Do While Timer < loopfinish 展开
展开全部
'给你个我写的延迟函数你用用..这个延迟函数过了午夜会失效1次,调用的时候用wait.小于0的数将暂,停按任意键继续
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Public Function GetKeys(ByVal Keys As KeyCodeConstants) As Boolean
GetKeys = GetAsyncKeyState(Keys) < 0
End Function
Public Function Wait(Optional ByVal Times)
On Error Resume Next
If Times = "-1" Then GoTo Hide
If Times >= "0" Then GoTo Counter
Hide:
Do
DoEvents
For i = 3 To 200
If GetKeys(i) And i <> 4 Then Wait = Timer + Times / 1000: Exit Do
Next
Loop
Counter:
Wait = Timer + Times / 1000
Do
DoEvents
If Timer >= Wait Then Exit Function
Loop
End Function
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Public Function GetKeys(ByVal Keys As KeyCodeConstants) As Boolean
GetKeys = GetAsyncKeyState(Keys) < 0
End Function
Public Function Wait(Optional ByVal Times)
On Error Resume Next
If Times = "-1" Then GoTo Hide
If Times >= "0" Then GoTo Counter
Hide:
Do
DoEvents
For i = 3 To 200
If GetKeys(i) And i <> 4 Then Wait = Timer + Times / 1000: Exit Do
Next
Loop
Counter:
Wait = Timer + Times / 1000
Do
DoEvents
If Timer >= Wait Then Exit Function
Loop
End Function
更多追问追答
追问
谢谢,但是我想知道的是为什么会出错,出错的原因找出来我才放心。
追答
你连代码都不帖出来,谁知道你哪里出错了.问问题有你这样的?让别人去猜?
展开全部
什么问题。另外Sleep是单线程的不能实现你这种多线程的要求。DoEvents可以用,但会使整个程序变得复杂不稳定。这段代码里的Timer系统计针没有考虑越过午夜的问题,你自己想想,午夜的86400就会变成0.
Dim SetTimeOut_Timer() As Double
Dim SetTimeOut_Event() As String
Private Sub Timer1_Timer()
Static pTimer As Double '前进入本事件的时间
Static nTimer As Double '现在进入本事件的时间
Static dTimer As Double '现在与前次进入本事件的时间差
nTimer = Timer
dTimer = nTimer - pTimer
pTimer = nTimer
u = UBound(SetTimeOut_Timer)
For i = 0 To u
If SetTimeOut_Event(i) <> "" Then
SetTimeOut_Timer(i) = SetTimeOut_Timer(i) - dTimer
If SetTimeOut_Timer(i) <= 0 Then
Call DoEvn(SetTimeOut_Event(i))
SetTimeOut_Event(i) = ""
End If
End If
Next i
End Sub
Private Sub Command1_Click()
SetTimeOut 5, "Command1_Click"
End Sub
Private Sub Command2_Click()
SetTimeOut 10, "Command2_Click"
End Sub
Private Sub Command3_Click()
SetTimeOut 15, "Command3_Click"
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
ReDim SetTimeOut_Timer(5)
ReDim SetTimeOut_Event(5)
Timer1.Interval = 100
End Sub
Public Function SetTimeOut(iSecond As Double, iEvent As String)
u = UBound(SetTimeOut_Timer)
idx = -1
For i = 0 To u
If SetTimeOut_Event(i) = "" Then
idx = i
Exit For
End If
Next i
If idx = -1 Then
ReDim Preserve SetTimeOut_Timer(u + 5)
ReDim Preserve SetTimeOut_Event(u + 5)
idx = u + 1
End If
SetTimeOut_Timer(idx) = iSecond
SetTimeOut_Event(idx) = iEvent
End Function
Public Sub DoEvn(iEvn As String)
Select Case iEvn
Case "Command1_Click"
Me.Print Time & " 5S到了"
Case "Command2_Click"
Me.Print Time & " 10S到了"
Case "Command3_Click"
Me.Print Time & " 15到了"
End Select
End Sub
Dim SetTimeOut_Timer() As Double
Dim SetTimeOut_Event() As String
Private Sub Timer1_Timer()
Static pTimer As Double '前进入本事件的时间
Static nTimer As Double '现在进入本事件的时间
Static dTimer As Double '现在与前次进入本事件的时间差
nTimer = Timer
dTimer = nTimer - pTimer
pTimer = nTimer
u = UBound(SetTimeOut_Timer)
For i = 0 To u
If SetTimeOut_Event(i) <> "" Then
SetTimeOut_Timer(i) = SetTimeOut_Timer(i) - dTimer
If SetTimeOut_Timer(i) <= 0 Then
Call DoEvn(SetTimeOut_Event(i))
SetTimeOut_Event(i) = ""
End If
End If
Next i
End Sub
Private Sub Command1_Click()
SetTimeOut 5, "Command1_Click"
End Sub
Private Sub Command2_Click()
SetTimeOut 10, "Command2_Click"
End Sub
Private Sub Command3_Click()
SetTimeOut 15, "Command3_Click"
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
ReDim SetTimeOut_Timer(5)
ReDim SetTimeOut_Event(5)
Timer1.Interval = 100
End Sub
Public Function SetTimeOut(iSecond As Double, iEvent As String)
u = UBound(SetTimeOut_Timer)
idx = -1
For i = 0 To u
If SetTimeOut_Event(i) = "" Then
idx = i
Exit For
End If
Next i
If idx = -1 Then
ReDim Preserve SetTimeOut_Timer(u + 5)
ReDim Preserve SetTimeOut_Event(u + 5)
idx = u + 1
End If
SetTimeOut_Timer(idx) = iSecond
SetTimeOut_Event(idx) = iEvent
End Function
Public Sub DoEvn(iEvn As String)
Select Case iEvn
Case "Command1_Click"
Me.Print Time & " 5S到了"
Case "Command2_Click"
Me.Print Time & " 10S到了"
Case "Command3_Click"
Me.Print Time & " 15到了"
End Select
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询