C# Program to Implement Sleep Method of Thread
Posted by Superadmin on August 11 2022 09:34:52

C# Program to Implement Sleep Method of Thread

 

This is a C# Program to implement sleep method of thread.

Problem Description

This C# Program Implements Sleep method of Thread.

Problem Solution

Here Sleep pauses programs. It receives a value indicating the number of milliseconds to wait.

Program/Source Code

Here is source code of the C# Program to Implement Sleep method of Thread. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement Sleep method of Thread
 */
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
    static void Main()
    {
        var stopwatch = Stopwatch.StartNew();
        Thread.Sleep(500);
        stopwatch.Stop();
        Console.WriteLine("Elapsed Milliseconds : {0}",
                          stopwatch.ElapsedMilliseconds);
        Console.WriteLine("Elapsed Ticks : {0}", stopwatch.ElapsedTicks);
        Console.WriteLine("Present Date and Time : {0}",
                          DateTime.Now.ToLongTimeString());
        Console.ReadLine();
    }
}
Program Explanation

In this C# Program, the Sleep() function is used to pauses program. It receives a value indicating the number of milliseconds to wait. Then ElapsedMilliseconds() function is used to print the Elapsed Milliseconds. The ElapsedTicks() function is used to print the Elapsed Ticks. The ToLongTimeString() function is used to print the Present Date and Time.

 
Runtime Test Cases
 
Elapsed Milliseconds : 498
Elapsed Ticks : 1231409
Present Date and Time : 8:36:06 PM