Jethro Land
Jethro Land

Reputation: 25

How to start a stopwatch with a loop and end when it ends in VB.NET

So I need to start a stopwatch with the push of a button which will also start a loop. How do I stop the stopwatch when the loop completes? Example below.

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

Sorry if that doesn't make any sense (I'm fairly new to this all). If there is anything I can do to help, I'm all ears.

Thank you all so much, SO is always super helpful with this all.

Upvotes: 0

Views: 249

Answers (1)

Blackwood
Blackwood

Reputation: 4534

You can start and stop a stopwatch and then read out the elapsed time in milliseconds like this.

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

Upvotes: 2

Related Questions