VB.NET Infinite For Loop

Is it possible to write an endless loop in VB.NET?

If so, what's the syntax?

-4


source to share


5 answers


For i as Integer = 0 To 1 Step 0

      

If that's not enough, just write:



For i As Integer = 0 To 2
  i -= 1
Next

      

+7


source


Do
    Something
Loop

      



+11


source


or

while (true)

end while

      


ok, correct For answer:

Dim InfiniteLoop as Boolean = true;
For i = 1 to 45687894

    If i = 45687893 And InfiniteLoop = true Then i = 1
End For

      

+4


source


Apart from all the many answers given to keep the loop running forever, this could only be the first one that actually uses the Positive Infinity value to limit the loop. However, to be on the safe side, I have included an additional option to exit after a certain number of seconds so that it can measure the speed of your cycle.

Sub RunInfinateForLoop(maxSeconds As Integer)
    ' Attempts to run a For loop to infinity but also exits if maxSeconds seconds have elapsed.
    Dim t As Date = Now
    Dim exitTime As Date = t.AddSeconds(maxSeconds)
    Dim dCounter As Double
    Dim strMessage As String
    For dCounter = 1 To Double.PositiveInfinity
        If Now >= exitTime Then Exit For
    Next
    strMessage = "Loop ended after " & dCounter.ToString & " loops in " & maxSeconds & " seconds." & vbCrLf &
        "Average speed is " & CStr(dCounter / maxSeconds) & " loops per second."
    MsgBox(strMessage, MsgBoxStyle.OkOnly, "Infinity Timer")

End Sub

      

+1


source


What I am doing is add a timer, then I change the interval to 1 and then I turn it on. If I want it to constantly check for something in a loop, I just double click on the timer for the timer_tick event, then I print what I want. I usually use this to update the settings if I want it to keep everything.

0


source







All Articles