home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Threading / WaitHandle / CS_Sample / MutexSample.cs < prev   
Encoding:
Text File  |  2000-06-23  |  4.0 KB  |  107 lines

  1. //==========================================================================
  2. //    File:        MutexSample.cs
  3. //
  4. //    Summary:    Example shows use of Mutex, AutoResetEvent and WaitHandle,
  5. //                        in threads.
  6. //                    Mutex.Mutex(bool,String)
  7. //                    Mutex.Mutex(bool)
  8. //                    Mutex.ReleaseMutex( )
  9. //                    Mutex.WaitAll(WaitHandle[])
  10. //                    Mutex.WaitAny(WaitHandle[])
  11. //                    Mutex.WaitOne(WaitHandle)
  12. //                    AutoResetEvent.AutoResetEvent(bool)
  13. //                    AutoResetEvent.Set( )
  14. //                    WaitHandle.WaitAll(WaitHandle[])
  15. //
  16. //----------------------------------------------------------------------------
  17. //    This file is part of the Microsoft NGWS Runtime Samples.
  18. //
  19. //    Copyright (C) 1998-2000 Microsoft Corporation.  All rights reserved.
  20. //===========================================================================
  21. using System;
  22. using System.Threading;
  23.  
  24. public class MutexSample
  25. {
  26.     static Mutex            gM1;
  27.     static Mutex            gM2;
  28.     const int                ITERS    = 100;
  29.     static AutoResetEvent    Event1    = new AutoResetEvent(false);
  30.     static AutoResetEvent    Event2    = new AutoResetEvent(false);
  31.     static AutoResetEvent    Event3    = new AutoResetEvent(false);
  32.     static AutoResetEvent    Event4    = new AutoResetEvent(false);
  33.     
  34.     public static void Main(String[] args)
  35.     {
  36.         Console.WriteLine("MutexSample.cs ...");
  37.         gM1 = new Mutex(true,"MyMutex");            //    create Mutext initialOwned, with name of "MyMutex"
  38.         gM2 = new Mutex(true);                        //    create Mutext initialOwned, with no name.
  39.         Console.WriteLine(" - Main Owns gM1 and gM2");
  40.  
  41.         AutoResetEvent[]    evs    = new AutoResetEvent[4];
  42.         evs[0] = Event1;            //    Event for t1
  43.         evs[1] = Event2;            //    Event for t2
  44.         evs[2] = Event3;            //    Event for t3
  45.         evs[3] = Event4;            //    Event for t4
  46.  
  47.         MutexSample            tm    = new MutexSample( );
  48.         Thread                t1    = new Thread(new ThreadStart(tm.t1Start));
  49.         Thread                t2    = new Thread(new ThreadStart(tm.t2Start));
  50.         Thread                t3    = new Thread(new ThreadStart(tm.t3Start));
  51.         Thread                t4    = new Thread(new ThreadStart(tm.t4Start));
  52.         t1.Start( );                //    Does Mutex.WaitAll(Mutex[] of gM1 and gM2)
  53.         t2.Start( );                //    Does Mutex.WaitOne(Mutex gM1)
  54.         t3.Start( );                //    Does Mutex.WaitAny(Mutex[] of gM1 and gM2)
  55.         t4.Start( );                //    Does Mutex.WaitOne(Mutex gM2)
  56.  
  57.         Thread.Sleep(2000);
  58.         Console.WriteLine(" - Main releases gM1");
  59.         gM1.ReleaseMutex( );        //    t2 and t3 will end and signal
  60.  
  61.         Thread.Sleep(1000);
  62.         Console.WriteLine(" - Main releases gM2");
  63.         gM2.ReleaseMutex( );        //    t1 and t4 will end and signal
  64.  
  65.         WaitHandle.WaitAll(evs);    //    waiting until all four threads signal that tey are done.
  66.         Console.WriteLine("... MutexSample.cs");
  67.     }
  68.  
  69.     public void t1Start( )
  70.     {
  71.         Console.WriteLine("t1Start started,  Mutex.WaitAll(Mutex[])");
  72.         Mutex[]                gMs    = new Mutex[2];
  73.         gMs[0] = gM1;                //    Create and load an array of Mutex for WaitAll call
  74.         gMs[1] = gM2;
  75.         Mutex.WaitAll(gMs);            //    Waits until both Mutex are released
  76.         Thread.Sleep(2000);
  77.         Console.WriteLine("t1Start finished, Mutex.WaitAll(Mutex[])");
  78.         Event1.Set( );                //    AutoResetEvent.Set( )    flagging method is done
  79.     }
  80.  
  81.     public void t2Start( )
  82.     {
  83.         Console.WriteLine("t2Start started,  gM1.WaitOne( )");
  84.         gM1.WaitOne( );                //    Waits until Mutex gM1 is released
  85.         Console.WriteLine("t2Start finished, gM1.WaitOne( )");
  86.         Event2.Set( );                //    AutoResetEvent.Set( )    flagging method is done
  87.     }
  88.  
  89.     public void t3Start( )
  90.     {
  91.         Console.WriteLine("t3Start started,  Mutex.WaitAny(Mutex[])");
  92.         Mutex[]                gMs    = new Mutex[2];
  93.         gMs[0] = gM1;                //    Create and load an array of Mutex for WaitAny call
  94.         gMs[1] = gM2;
  95.         Mutex.WaitAny(gMs);            //    Waits until either Mutex is released
  96.         Console.WriteLine("t3Start finished, Mutex.WaitAny(Mutex[])");
  97.         Event3.Set( );                //    AutoResetEvent.Set( )    flagging method is done
  98.     }
  99.  
  100.     public void t4Start( )
  101.     {
  102.         Console.WriteLine("t4Start started,  gM2.WaitOne( )");
  103.         gM2.WaitOne( );                //    Waits until Mutex gM2 is released
  104.         Console.WriteLine("t4Start finished, gM2.WaitOne( )");
  105.         Event4.Set( );                //    AutoResetEvent.Set( )    flagging method is done
  106.     }
  107. }