如何在Visual Basic 6中创建30秒的延迟?我只是希望VB6等待30秒,然后再继续执行下一行代码!
我知道在VB6中完成此操作的最佳方法是在循环中包含对WaitForSingleObject的调用或其他一些类似的Wait API函数。这种方法的一个很好的例子是Sergey Merzlikin(source article)编写的MsgWaitObj函数:
Option Explicit
'********************************************
'* (c) 1999-2000 Sergey Merzlikin *
'********************************************
Private Const STATUS_TIMEOUT = &H102&
Private Const INFINITE = -1& ' Infinite interval
Private Const QS_KEY = &H1&
Private Const QS_MOUSEMOVE = &H2&
Private Const QS_MOUSEBUTTON = &H4&
Private Const QS_POSTMESSAGE = &H8&
Private Const QS_TIMER = &H10&
Private Const QS_PAINT = &H20&
Private Const QS_SENDMESSAGE = &H40&
Private Const QS_HOTKEY = &H80&
Private Const QS_ALLINPUT = (QS_SENDMESSAGE Or QS_PAINT _
Or QS_TIMER Or QS_POSTMESSAGE Or QS_MOUSEBUTTON _
Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)
Private Declare Function MsgWaitForMultipleObjects Lib "user32" _
(ByVal nCount As Long, pHandles As Long, _
ByVal fWaitAll As Long, ByVal dwMilliseconds _
As Long, ByVal dwWakeMask As Long) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
' The MsgWaitObj function replaces Sleep,
' WaitForSingleObject, WaitForMultipleObjects functions.
' Unlike these functions, it
' doesn't block thread messages processing.
' Using instead Sleep:
' MsgWaitObj dwMilliseconds
' Using instead WaitForSingleObject:
' retval = MsgWaitObj(dwMilliseconds, hObj, 1&)
' Using instead WaitForMultipleObjects:
' retval = MsgWaitObj(dwMilliseconds, hObj(0&), n),
' where n - wait objects quantity,
' hObj() - their handles array.
Public Function MsgWaitObj(Interval As Long, _
Optional hObj As Long = 0&, _
Optional nObj As Long = 0&) As Long
Dim T As Long, T1 As Long
If Interval <> INFINITE Then
T = GetTickCount()
On Error Resume Next
T = T + Interval
' Overflow prevention
If Err <> 0& Then
If T > 0& Then
T = ((T + &H80000000) _
+ Interval) + &H80000000
Else
T = ((T - &H80000000) _
+ Interval) - &H80000000
End If
End If
On Error GoTo 0
' T contains now absolute time of the end of interval
Else
T1 = INFINITE
End If
Do
If Interval <> INFINITE Then
T1 = GetTickCount()
On Error Resume Next
T1 = T - T1
' Overflow prevention
If Err <> 0& Then
If T > 0& Then
T1 = ((T + &H80000000) _
- (T1 - &H80000000))
Else
T1 = ((T - &H80000000) _
- (T1 + &H80000000))
End If
End If
On Error GoTo 0
' T1 contains now the remaining interval part
If IIf((T1 Xor Interval) > 0&, _
T1 > Interval, T1 < 0&) Then
' Interval expired
' during DoEvents
MsgWaitObj = STATUS_TIMEOUT
Exit Function
End If
End If
' Wait for event, interval expiration
' or message appearance in thread queue
MsgWaitObj = MsgWaitForMultipleObjects(nObj, _
hObj, 0&, T1, QS_ALLINPUT)
' Let's message be processed
DoEvents
If MsgWaitObj <> nObj Then Exit Function
' It was message - continue to wait
Loop
End Function
Sub delay()
Dim a As Integer = TimeOfDay.Second
Do Until TimeOfDay.Second = a + 30
Loop
End Sub
请记住,@ sanderd答案中的Sleep解决方案实际上将锁定该应用程序。换句话说,所有的UI片都将无响应。
如果您的目标是在允许UI响应的同时只是简单地防止控件继续前进到下一行,则还有其他选择。
一个将以以下方式循环30秒:
' Module Level
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
' Inside a method
Dim dt as Date
dt = Now
Do While DateDiff("s", dt, now) < 30
DoEvents
Sleep 50 ' put your app to sleep in small increments
' to avoid having CPU go to 100%
Loop
这不是实现所需目标的最优雅的方法,但是可以完成工作。
System.Threading.Thread.Sleep(30*1000)
((@ Moox我的坏;))
请注意,在用户界面线程中调用此方法将在等待30秒时挂起整个应用程序。更好的选择是为要执行的代码产生一个新线程。
edit:由于您还有其他关于VB6的问题,因此以下是提供VB6睡眠方法的链接:http://www.freevbcode.com/ShowCode.Asp?ID=7556
System.Threading.Thread.Sleep(100)
为了一套完整的解决方案:
如果您在应用程序环境(例如excel)中使用vb6,则可以使用
Application.OnTime (now + TimeValue("00:00:30")), "ProcedurePart2"
在30秒后调用过程(无参数),而不会锁定应用程序或不消耗CPU功率。
例如,它将在任何VBA环境中工作。根据您的VB6应用程序的性质(独立vs加载项),您可以使用此选项。
将其余代码放在一个子程序中,然后初始化30秒计时器以调用该子程序。在退出之前自然禁用计时器。
怎么样?
Public Sub delay(PauseTime as integer)
Dim start As single
start = Timer
Do While Timer < start + PauseTime
If (Timer < start) Then 'midnight crossover
start = start - (86400 + 1)
End If
DoEvents ' Yield to other processes.
Loop
End Sub
我认为Timer是最好的解决方案,但是有一些修改。
禁用计时器的唯一方法是引发一个事件以执行此操作。示例:
'Global Declaration
Private event TimeReached()
dim counter as integer
Sub Timer_Tick () handles Timer.tick
if counter>1 then
RaiseEvent TimeReached
else
counter+=1
end sub
sub TimeHandler () handles me.TimeReached
Timer.enabled=false
'Code To be Processed Here
end sub
我尝试了太多不同的方法来进行延迟,这是我所发现的最快的方法
这是一个在单击按钮30秒后显示消息的程序。
sub button1_click () handles button1.click
Dim instance =now
do while now.subtract(instance).seconds<30
loop
msgbox("30 seconds passed")
endsub