VB.Net Tutorial/Date Time/Stopwatch

Материал из VB Эксперт
Перейти к: навигация, поиск

StopWatch: ElapsedMilliseconds

Public Class Tester
    Public Shared Sub Main
        Dim lastMillis As Long
        Dim numMillis As Long
        Dim testWatch As New System.Diagnostics.Stopwatch
        Dim endTime As Date
        Dim results As String
        endTime = Now.AddSeconds(1)
        testWatch.Start()
        Do
            If (testWatch.ElapsedMilliseconds <> lastMillis) Then
                numMillis += 1
                lastMillis = testWatch.ElapsedMilliseconds
            End If
        Loop Until (Now > endTime)
        results = "Elapsed milliseconds:  " & _
           testWatch.ElapsedMilliseconds.ToString & vbNewLine & _
           "Number of updates per second: " & numMillis.ToString
        Console.WriteLine(results)
    End Sub
End Class
Elapsed milliseconds:  1001
Number of updates per second: 1001

Use StopWatch

Public Class Tester
    Public Shared Sub Main
        Dim testWatch As New System.Diagnostics.Stopwatch
        Dim results As String
        " ----- Start counting.
        testWatch.Start()
        MsgBox("Press OK to see elapsed seconds")
        " ----- Stop and record.
        results = String.Format( _
           "testWatch.Elapsed.Seconds: {0}{3}" & _
           "testWatch.Elapsed.TotalSeconds: {1}{3}" & _
           "testWatch.ElapsedMilliseconds / 1000: {2}", _
           testWatch.Elapsed.Seconds, _
           testWatch.Elapsed.TotalSeconds, _
           testWatch.ElapsedMilliseconds / 1000, vbNewLine)
        Console.WriteLine(results)
    End Sub
End Class
testWatch.Elapsed.Seconds: 2
testWatch.Elapsed.TotalSeconds: 2.2114405
testWatch.ElapsedMilliseconds / 1000: 2.211