Languages
[Edit]
EN

C#/.NET - thread join

3 points
Created by:
AnnLen
9180

In C#/.NET it is possible to wait with Thread.Join method until some thread will not finish working.

Thread.Join method example.

using System;
using System.Threading;

namespace Test
{
	class Program
	{
		static void Main(string[] args)
		{
			Thread thread = new Thread(() =>
			{
				Console.WriteLine("Thread before sleeping...");
				Thread.Sleep(2000);
				Console.WriteLine("Thread after sleeping...");
			});

			thread.Start();
			Console.WriteLine("Before waiting...");
			thread.Join();
			Console.WriteLine("After waiting...");
		}
	}
}

Where:

  • Thread.Join method - sleeps current code until executed thread will not finish working

Output:

Thread before sleeping...
Before waiting...
Thread after sleeping...
After waiting...

References

  1. Thread Class - Microsoft Docs
  2. Thread.Join method - Microsoft Docs

See also

  1. C#/.NET - create thread
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