我在 VB6 中有一个长时间运行的进程,我想在执行下一行代码之前完成它。我怎样才能做到这一点?内置功能?我可以控制等待多长时间吗?
简单的例子:
Call ExternalLongRunningProcess
Call DoOtherStuff
如何延迟“DoOtherStuff”?
虽然 Nescio 的答案(DoEvents) 会起作用,但它会导致您的应用程序使用 100% 的 CPU。睡眠将使 UI 无响应。您需要的是两者的结合,而似乎效果最好的神奇组合是:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
While IsStillWaitingForSomething()
DoEvents
DoEvents
Sleep(55)
Wend
为什么有两个 DoEvent,一个休眠 55 毫秒?55 毫秒的睡眠是 VB6 可以处理的最小时间片,并且在需要超级响应能力的情况下有时需要使用两个 DoEvents(不是通过 API,而是如果您的应用程序正在响应外部事件、SendMessage、Interupts 等) )。
VB.Net:我会使用WaitOne事件句柄。
VB 6.0:我见过一个 DoEvents 循环。
Do
If isSomeCheckCondition() Then Exit Do
DoEvents
Loop
最后,你可以睡觉了:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sleep 10000
将您的代码分成 2 个进程。运行第一个,然后运行“长时间运行的进程”,然后运行第二个进程。
在当前进程的中间运行长时间运行的进程并等待它完成。
我希望您可以将 .net 框架 system.dll 或其他任何内容添加到您的项目引用中,以便您可以这样做:
Dim ALongTime As Integer = 2000
System.Threading.Thread.Sleep(ALongTime)
...每次。我的机器上有 VB6 和 VB.net 2008,我总是很难在非常不同的 IDE 之间切换。
如果你想写一个sleep
或wait
不声明sleep
你可以写一个使用系统定时器的循环。这是我在运行解释器时用于测试/调试的。如果您需要这样的东西,可以在解释器暂停时添加它:
Dim TimeStart as currency
Dim TimeStop as currency
Dim TimePassed as currency
Dim TimeWait as currency
'use this block where you need a pause
TimeWait = 0.5 'seconds
TimeStart = Timer()
TimePassed = 0
Do while TimePassed < TimeWait 'seconds
TimeStop = timer()
TimePassed = TimeStop - TimeStart
doevents
loop
System.Threading.Thread.Sleep(500)