Languages
[Edit]
EN

C#/.NET - sleep thread

10 points
Created by:
Nabila-Burnett
385

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# - Thread Class

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join