VB.NET Infinite For Loop
5 answers
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 to share
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 to share