C# Thread Sleep

C# Thread Sleep

In my last post we saw a simple program about threading in C#. Here we will see how to sleep current running thread for some time in milliseconds.

C# Thread Sleep Example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleDemo.App
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("This thread will sleep for 1000 milliseconds.");

            Thread.Sleep(1000);

            Console.WriteLine("Print after 1000 milliseconds.");

            Console.ReadLine();
        }

        static void MethodA()
        {
            Thread.Sleep(1000);
            Console.WriteLine("Thread Method A");
        }
        
    }
}

Output

This thread will sleep for 1000 milliseconds.
Print after 1000 milliseconds.

You may require to sleep the thread to perform long running operation. Better way use asynch call backs.

Threading in C#C# foreach loopC# DateTimec# arraylistc# stringC# DateTime DifferenceC# XML XmlReaderC# Binary To StringC# Regular Expression Get Text Between BracketsC# Generate Random NumberReplace string in C#

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.