VB.NET 无限循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/583177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:05:21  来源:igfitidea点击:

VB.NET Infinite For Loop

vb.netfor-loopinfinite-loop

提问by user62572

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

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

If so, what is the syntax?

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

回答by erikkallen

Do
    Something
Loop

回答by Yes - that Jake.

For i as Integer = 0 To 1 Step 0

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

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

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

回答by cjk

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

回答by Joseph Clovis

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

回答by Charles

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.

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