Languages
[Edit]
EN

C#/.NET - sleep thread

10 points
Created by:
AnnLen
9120

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
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join