如何在visual basic中创建30秒的延迟.我只是希望VB等待30秒才能进入下一行代码!!
我知道在VB6中实现这一目标的最佳方法是在循环中包含对WaitForSingleObject或其他类似Wait API函数的调用.这种方法的一个很好的例子是Sergey Merzlikin编写的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
Run Code Online (Sandbox Code Playgroud)
请记住,@ 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
Run Code Online (Sandbox Code Playgroud)
这不是实现你想要的最优雅的方式,但它完成了工作.
System.Threading.Thread.Sleep(30*1000) (@Moox 我的错;))
请注意,在 UI 线程中调用此方法将挂起整个应用程序,同时等待 30 秒。更好的选择是为您想要执行的代码生成一个新线程。
编辑:
由于您的其他问题是关于 VB6 的,这里有一个提供 VB6 Sleep 方法的链接:
http://www.freevbcode.com/ShowCode.Asp ?ID=7556
| 归档时间: |
|
| 查看次数: |
64241 次 |
| 最近记录: |