如何在visual basic中创建30秒的延迟。我只是希望VB等待30秒才能转到下一行代码!!
答案 0 :(得分:7)
我知道在VB6中完成此操作的最佳方法是在循环中包含对WaitForSingleObject或其他类似Wait API函数的调用。这种方法的一个很好的例子是Sergey Merzlikin编写的MsgWaitObj函数(source article):
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
答案 1 :(得分:6)
请记住,@ 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
这不是达到你想要的最优雅的方式,但它可以完成工作。
答案 2 :(得分:4)
System.Threading.Thread.Sleep(30*1000)
(@ Moox my bad;))
请注意,在UI线程中调用此方法将在等待30秒时挂起整个应用程序。一个更好的选择是为你想要执行的代码生成一个新线程。
修改强>
由于您的其他问题是关于VB6,这里有一个提供VB6睡眠方法的链接:
http://www.freevbcode.com/ShowCode.Asp?ID=7556
答案 3 :(得分:2)
System.Threading.Thread.Sleep(100)
答案 4 :(得分:1)
为了一套完整的解决方案:
如果您在应用程序环境(如excel)中使用vb6,则可以使用
Application.OnTime (now + TimeValue("00:00:30")), "ProcedurePart2"
在30秒后调用一个过程(没有参数),而不会锁定应用程序或消耗CPU电源。
例如,这可以在任何VBA环境中使用。根据VB6应用程序的性质(独立版和附加版),您可以使用此选项。
答案 5 :(得分:1)
将其余代码放入sub中,然后初始化30秒计时器以调用该sub。退出前自然停用计时器。
答案 6 :(得分:1)
这个怎么样?
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
答案 7 :(得分:1)
我认为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
答案 8 :(得分:1)
我尝试了太多不同的方法来延迟,这是我发现的最快的方法
这是一个在单击按钮后30秒后显示消息的程序。
sub button1_click () handles button1.click
Dim instance =now
do while now.subtract(instance).seconds<30
loop
msgbox("30 seconds passed")
endsub