/*
* 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.
Program:
Output:
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.