user62572
user62572

Reputation: 1388

VB.NET Infinite For Loop

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

If so, what is the syntax?

Upvotes: 0

Views: 33440

Answers (5)

Joseph Clovis
Joseph Clovis

Reputation: 51

Aside from all the many answers given to make a loop run forever, this may just be the first that actually uses the value of Positive Infinity to cap the loop. Just to be safe though, I included an extra option to exit after a given number of seconds so it can measure the speed of your loop.

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

Upvotes: 2

Charles
Charles

Reputation: 405

What I do is add a timer then I change the interval to 1 and then I make it enabled then If I want it to constantly check something through the loop I just double click the timer for the timer_tick event then I type what I want. I usually use this for updating the settings if I want it to save every thing.

Upvotes: 0

cjk
cjk

Reputation: 46485

or

while (true)

end while

ok, proper For answer:

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

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

Upvotes: 6

Yes - that Jake.
Yes - that Jake.

Reputation: 17129

For i as Integer = 0 To 1 Step 0

If that's not hacky enough, can also write:

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

Upvotes: 9

erikkallen
erikkallen

Reputation: 34421

Do
    Something
Loop

Upvotes: 17

Related Questions