AlexF11 AlexF11 - 1 year ago 173
Vb.net Question

Run Function until true is returned OR 15 seconds has passed

I have a method that needs to rerun until it either returns True or a set time period has passed. Would a cancellation token be the way to do this? A stopwatch? A timer?

Do While partReady = False
partReady = readTag(part, "_IO_EM_DI_04")
Loop


Runs it until I get true returned but I need this to cancel and throw and exception or exit the loop after a given time period.

Answer Source

Use Stopwatch:

    Dim sw As New Stopwatch
    sw.Start()

    Do While partReady = False
        partReady = readTag(part, "_IO_EM_DI_04")
        If sw.Elapsed.TotalSeconds = 15 Then
            Exit Do
        End If
    Loop
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download