Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now