C# program to print elapsed milliseconds of stopwatch using Thread.Sleep() method

Here, we are going to learn how to print elapsed milliseconds of stopwatch using Thread.Sleep() method in C#?
Submitted by Nidhi, on August 16, 2020

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:

/*
 * Program to print elapsed milliseconds of Stopwatch 
 * using Thread.Sleep() method in C#
 */

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static void Main()
    {
        Stopwatch watch = Stopwatch.StartNew();
        
        Thread.Sleep(2000);
        watch.Stop();

        Console.WriteLine("Elapsed Milliseconds by ThreadSleep() :"+watch.ElapsedMilliseconds);
    }
}

Output:

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

Explanation:

In the above program, we created a program class that contains the Main() method. In the Main() method we created an object watch of StopWatch class and start the stopwatch using StatNew() method. Then paused the Main() thread for 2000 milliseconds then stop the stopwatch and print the elapsed time using ElapsedMilliseconds property on the console screen.

C# Thread Programs »


ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.