VB.Net program to print the elapsed milliseconds of stopwatch using Thread.Sleep() method

Here, we are going to learn how to print the elapsed milliseconds of stopwatch using Thread.Sleep() method in VB.Net?
Submitted by Nidhi, on January 04, 2021 [Last updated : March 08, 2023]

How to get elapsed milliseconds of stopwatch in VB.Net?

To solve the above problem, here we paused the program using Thread.Sleep() and then print elapsed milliseconds using ElapsedMilliseconds property of StopWatch class.

Program/Source Code:

The source code to print the elapsed milliseconds of stopwatch using Thread.Sleep() method is given below. The given program is compiled and executed successfully.

VB.Net code to print the elapsed milliseconds of stopwatch using Thread.Sleep() method

'Vb.Net program to print the elapsed milliseconds of 
'stopwatch using Thread.Sleep() method.

Imports System.Threading

Module Module1
    Sub Main()
        Dim watch As Stopwatch = Stopwatch.StartNew()

        Thread.Sleep(2000)
        watch.Stop()

        Console.WriteLine("Elapsed Milliseconds by ThreadSleep() : {0}", watch.ElapsedMilliseconds)
    End Sub
End Module

Output

Elapsed Milliseconds by ThreadSleep() : 2007
Press any key to continue . . .

Explanation

In the above program, we imported the System.Threading namespace to implement multithreading in the program. After that, we created a module Module1. The Module1 contains Main() function.

The Main() is the entry point for the program, here we created the object of Stopwatch class using StartNew() method of StopWatch class. After that, we slept the thread for 2000 milliseconds using the Sleep() method and then stop the thread and print the elapsed time in milliseconds using the ElapsedMilliseconds property of Stopwatch class on the console screen.

VB.Net Threading Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.