Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for a file to be created with a timeout

I am trying to make a vb6 prog to wait for the creation of a pdf file. Right now I'm just making a pause of 3 sec like that :

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

but some times the pdf creation takes more that 3 sec. So I'd like to wait for the file to be created but with a timeout (says 10sec). I prefer not to extend the waiting time as this will be run a thousand times.

like image 333
Loïc MICHEL Avatar asked Oct 30 '22 19:10

Loïc MICHEL


1 Answers

You can use Sleep with 1000 ms, it means it will wait for 1 second until it keeps running the code, using a flag variable called sTimeout you can define a number of seconds it will run the loop, I hard coded 10 but you can make another variable for setting the seconds, every second it will run the loop and increase sTimeout by one, once it reaches 10 it will finish the while loop.

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
like image 96
MadAntrax Avatar answered Nov 10 '22 16:11

MadAntrax