Block until a thread finishes, or timeout after 2 seconds
using System;
using System.Threading;
class MainClass
{
private static void DoCount()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("{0} : DoCount thread",DateTime.Now.ToString("HH:mm:ss.ffff"));
// Sleep for 1 second.
Thread.Sleep(1000);
}
}
public static void Main()
{
Thread thread = new Thread(DoCount);
Console.WriteLine("{0} : Starting DoCount thread.",DateTime.Now.ToString("HH:mm:ss.ffff"));
thread.Start();
if (!thread.Join(2000))
{
Console.WriteLine("{0} : Join timed out !!", DateTime.Now.ToString("HH:mm:ss.ffff"));
}
// Block again until the DoCount thread finishes with no timeout.
thread.Join();
}
}