Create Stopwatch using VB.NET

Stopwatch class use to measure time for interval.
  • 8328
Download Files:
 

You can create stopwatch using stopwatch class. Stopwatch class use to measure time for interval. Start method use to measure start elapsed time for an interval and Stop method use to measure stop elapsed time for an interval. You can also use reset method to set time elapsed to zero and starts measuring elapsed time. You can also determine the execution of time using Imports System.Threading class in your program.

Code

Imports System.Threading
Module
Module1
     Sub Main()
        Dim stopwatch1 As New System.Diagnostics.Stopwatch

        stopwatch1.Start()
        For i As Integer = 0 To 999
            Thread.Sleep(2)
        Next
        stopwatch1.[Stop]()
        Console.WriteLine()
        Console.WriteLine("Time elapsed: {0}", stopwatch1.Elapsed)
        Console.WriteLine()
        Dim stopwatch2 As New System.Diagnostics.Stopwatch()
        Dim results As String = Nothing
        stopwatch2.Start()
        Dim ts As TimeSpan = stopwatch1.Elapsed
        results = String.Format("{0} minute(s)" & " {1} second(s)" & " {2} milesconds(s)", ts.Minutes, ts.Seconds, ts.Milliseconds)
        Console.WriteLine(results)
        stopwatch2.[Stop]()
        Console.ReadLine()
    End Sub

End
Module

Output

Untitled-1.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.