Waiting with WaitHandle : WaitHandle : Thread C# Examples


C# Examples » Thread » WaitHandle »

 

Waiting with WaitHandle









    
using  System;
using  System.Threading;

class  ThreadSleeper
{
        AutoResetEvent  napDone  =  new  AutoResetEvent(false);
        
        public  void  Sleep()
        {
                Thread.Sleep(1000);
                napDone.Set();
        }
        
        public  static  WaitHandle  GetWaitHandle()
        {
                ThreadSleeper  ts  =  new  ThreadSleeper();
                Thread  thread  =  new  Thread(new  ThreadStart(ts.Sleep));
                thread.Start();
                return(ts.napDone);
        }
}

class  MainClass
{
        public  static  void  Main()
        {
                WaitHandle[]  waits  =  new  WaitHandle[2];
                waits[0]  =  ThreadSleeper.GetWaitHandle();
                waits[1]  =  ThreadSleeper.GetWaitHandle();
                
                Console.WriteLine("Waiting  for  threads  to  finish");
                WaitHandle.WaitAll(waits);
                Console.WriteLine("Threads  finished");
        }
}
    
   
  
   



Output

Waiting for threads to finish
Threads finished


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Thread
» WaitHandle