Languages
[Edit]
EN

C#/.NET - sleep thread

10 points
Created by:
AnnLen
1789

To pause thread in C#/.NET Thread.Sleep method can be used.

using System;
using System.Threading;

namespace Test
{
	class Program
	{
		static void Main(string[] args)
		{
			Thread thread = new Thread(() =>
			{
				Console.WriteLine("My thread: step 1");
				Thread.Sleep(1000);
				Console.WriteLine("My thread: step 2");
				Thread.Sleep(1000);
				Console.WriteLine("My thread: step 3");
			});

			thread.Start();

			Console.WriteLine("Main thread: step 1");
			Thread.Sleep(1000);
			Console.WriteLine("Main thread: step 2");
			Thread.Sleep(1000);
			Console.WriteLine("Main thread: step 3");
		}
	}
}

Output:

Main thread: step 1
My thread: step 1
Main thread: step 2
My thread: step 2
Main thread: step 3
My thread: step 3
Note: Thread.Sleep sleeps thread in which method has been called for some milliseconds.

References:

  1. Thread.Sleep Method - Microsoft Docs
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.