EN
C#/.NET - wait for thread to finish
7 points
In C#/.NET it is few ways to wait for thread finished.
See this link.
xxxxxxxxxx
1
using System;
2
using System.Threading;
3
4
namespace Test
5
{
6
class Program
7
{
8
static void Main(string[] args)
9
{
10
Object locker = new Object();
11
12
Thread thread = new Thread(() =>
13
{
14
Thread.Sleep(2000);
15
16
lock (locker)
17
Monitor.Pulse(locker);
18
});
19
20
thread.Start();
21
22
lock(locker)
23
Monitor.Wait(locker);
24
25
Console.WriteLine("Thread job done...");
26
}
27
}
28
}
Output:
xxxxxxxxxx
1
Thread job done...
Notes:
Monitor.Wait
andMonitor.Pulse
methods should be called from lock block.Monitor.Wait
method waits untilMonitor.Pulse
method is called.