Loï*_*HEL 5 filesystems vb6 timeout wait
我正在尝试制作一个vb6 prog来等待创建一个pdf文件.现在我只是暂停3秒:
startTime = Time
endTime = TimeValue(startTime) + TimeValue(TimeSerial(0,0,3))
While endTime > Time
Wend
If FSO.FileExists(sPdfFileName) Then
OkCreatedPDF = True
Else
OkCreatedPDF = False
End If
Run Code Online (Sandbox Code Playgroud)
但有时候pdf创作花费的时间超过3秒.所以我想等待创建文件,但是超时(10秒).我不想延长等待时间,因为这将会运行一千次.
小智 2
您可以使用Sleep1000 ms,这意味着它将等待 1 秒,直到继续运行代码,使用名为的标志变量sTimeout您可以定义它将运行循环的秒数,我硬编码为 10 但您可以创建另一个变量对于设置秒数,每秒都会运行一次循环并加sTimeout一,一旦达到 10 就会结束while循环。
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Function GeneratePDF()
Dim sTimeout as Integer
Call YourPDFroutine()
StatusLabel.Caption = "Wait until PDF is finished..."
While FSO.FileExists(sPdfFileName) = False
sTimeout = sTimeout + 1
Sleep 1000
If sTimeOut > 10 Then
OkCreatedPDF = False
StatusLabel.Caption = "ERROR: Timeout!"
Exit Function
End If
Wend
OkCreatedPDF = True
StatusLabel.Caption = "The PDF " & sPdfFileName & " was generated!"
End Function
Run Code Online (Sandbox Code Playgroud)