EN
C#/.NET - wait for thread to finish
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:
Monitor.WaitandMonitor.Pulsemethods should be called from lock block.Monitor.Waitmethod waits untilMonitor.Pulsemethod is called.