VB.NET Infinite For Loop

2020-05-09 22:32发布

问题:

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

If so, what is the syntax?

回答1:

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


回答2:

Do
    Something
Loop


回答3:

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


回答4:

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


回答5:

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.