代码之家  ›  专栏  ›  技术社区  ›  Jethro Land

如何在VB.NET中使用循环启动秒表并在秒表结束时结束

  •  0
  • Jethro Land  · 技术社区  · 11 年前

    所以我需要按下一个按钮启动秒表,这也会启动一个循环。循环完成后如何停止秒表?示例如下。

    Private Sub btnStart...
        n = 0
        While n < 1001
            n += 1
            (Contents of loop go here)
        End While
        Stop stopwatch
        lbl1.Text = stopwatchvalue
    

    抱歉,如果这没有任何意义(我对这一切都很陌生)。如果有什么我能帮忙的,我会全神贯注的。

    非常感谢大家,所以这一切总是非常有帮助。

    1 回复  |  直到 11 年前
        1
  •  2
  •   Blackwood    11 年前

    你可以启动和停止秒表,然后像这样以毫秒为单位读出经过的时间。

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sw As New Stopwatch
        sw.Start()
        Dim n As Integer = 0
        Do While n < 1001
            n += 1
            '(Contents of loop go here)
        Loop
        sw.Stop()
        lbl1.Text = sw.ElapsedMilliseconds.ToString
    End Sub