-1

是否可以在 VB.NET 中编写无限 for 循环?

如果是这样,语法是什么?

4

5 回答 5

17
Do
    Something
Loop
于 2009-02-24T19:26:36.380 回答
9
For i as Integer = 0 To 1 Step 0

如果这还不够hacky,还可以写:

For i As Integer = 0 To 2
  i -= 1
Next
于 2009-02-24T19:25:12.937 回答
6

或者

while (true)

end while

好的,正确的答案:

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

    If i = 45687893 And InfiniteLoop = true Then i = 1
End For
于 2009-02-24T19:26:55.210 回答
2

除了为使循环永远运行而给出的所有答案之外,这可能只是第一个实际使用正无穷大的值来限制循环的方法。不过为了安全起见,我添加了一个额外的选项,可以在给定的秒数后退出,这样它就可以测量循环的速度。

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
于 2016-09-27T07:04:42.547 回答
0

我所做的是添加一个计时器,然后将间隔更改为 1,然后启用它,然后如果我希望它通过循环不断检查某些内容,我只需双击 timer_tick 事件的计时器,然后输入我想要的内容。如果我希望它保存所有内容,我通常使用它来更新设置。

于 2013-10-02T01:24:28.967 回答