To achieve this we'll utilize the WaitOne() method of AutoResetEvent class. It'll wait until it gets notified from secondary thread for completion and this I think should be the optimized way for wait here.
Let see the example:
using System;
using System.Threading;
namespace Threads_CSHandler_AutoResetEvent
{
class Program
{
//Declare the wait handler
private static AutoResetEvent waitHandle = new AutoResetEvent(false);
static void Main(string[] args)
{
Console.WriteLine("Main() running on thread {0}", Thread.CurrentThread.ManagedThreadId);
Program p = new Program();
Thread t = new Thread(new ThreadStart(p.PrintNumbers));
t.Start();
//Wait untill get notified
waitHandle.WaitOne();
Console.WriteLine("\nSecondary thread is done!");
Console.Read();
}
Read more: C# Corner
QR: