To achieve this we'll utilize the WaitOne() method of the AutoResetEvent class. It'll wait until it gets notified from a secondary thread for completion and this I think should be the optimized way for waiting 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();
}
public void PrintNumbers()
{
Console.WriteLine(" ");
Console.WriteLine("Executing Thread {0}", Thread.CurrentThread.ManagedThreadId);
for (int i = 1; i <= 10; i++)
{
//Just to consume some time in operation
Thread.Sleep(100);
Console.Write(i + " ");
}
//Tell other other thread that we're done here
waitHandle.Set();
}
}
}


Amit ChoudharyeditedPosted Jul 25, 2011, 2:54 PMEdited Jul 25, 2011, 2:56 PM
Thanks Sam.. I appreciate your views here...well in order to exploring the MultiThreading concepts I just found some wonderful facts and gave a thought to share it.. if you saying then i'll try to write an extension for this article anyway.. :)
Sam HobbsPosted Jul 25, 2011, 1:49 PM
Good article; I certainly have used events many times. This is an article that I might have written if I thought of it. I think a bit more explanation would help beginners.