Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

C#/.NET - wait for thread to finish

0 contributions
7 points

In C#/.NET it is few ways to wait for thread finished.

1. Thread.Join method example

See this link.

2. Monitor.Pulse and Monitor.Wait example

using System;
using System.Threading;

namespace Test
{
	class Program
	{
		static void Main(string[] args)
		{
			Object locker = new Object();

			Thread thread = new Thread(() =>
			{
				Thread.Sleep(2000);

				lock (locker)
					Monitor.Pulse(locker);
			});

			thread.Start();

			lock(locker)
				Monitor.Wait(locker);

			Console.WriteLine("Thread job done...");
		}
	}
}

Output:

Thread job done...
Notes:
  1. Monitor.Wait and Monitor.Pulse methods should be called from lock block.
  2. Monitor.Wait method waits until Monitor.Pulse method is called.

References

  1. Thread Class - Microsoft Docs
  2. Monitor.Wait Method - Microsoft Docs
  3. Monitor.Pulse Method - Microsoft Docs
0 contributions

Suggested wiki articles:

Suggested findings

Let's do the work and have some fun
  • Dirask is online IT community for professionals and hobbyist 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,
    we will help you. 👍 ✔ 🦄
Read more