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

  1. //==========================================================================
  2. //    File:        ThdRelStaticSample.cs
  3. //
  4. //    Summary:    Example shows how to use the attribute [ThreadStatic( )]
  5. //                    to make a static field be a Thread Relative Static.
  6. //
  7. //    Note:        If you compile this sample with the "/D:WITHOUT" switch
  8. //                    you will see mainThd.thd1, mainThd.thd2, and mainThd
  9. //                    all changing the same field "E.stat" on each other.
  10. //
  11. //----------------------------------------------------------------------------
  12. //    This file is part of the Microsoft NGWS Runtime Samples.
  13. //
  14. //    Copyright (C) 1998-2000 Microsoft Corporation.  All rights reserved.
  15. //===========================================================================
  16. using System;
  17. using System.Threading;
  18.  
  19. public class ThdRelStaticSample
  20. {
  21.     public E        E1;                    //    object E1 will be one instance of class E
  22.     public E        E2;                    //    object E2 will be another instance of class E
  23.     public Thread    thd1;                //    Thread thd1 will be running on object E1, method Run1
  24.     public Thread    thd2;                //    Thread thd1 will be running on object E1, method Run2
  25.  
  26.     public ThdRelStaticSample( ) {
  27.         E1 = new E( );
  28.         E2 = new E( );
  29.         thd1 = new Thread(new ThreadStart(E1.Run1));
  30.         thd2 = new Thread(new ThreadStart(E1.Run2));
  31.         thd1.Start( );
  32.         thd2.Start( );
  33.     }
  34.  
  35.     static public void Main(String[] args) {
  36. #if        !WITHOUT
  37.         Console.WriteLine("Thread Relative Statics Sample  with \"[ThreadStaticAttribute( )]\"");
  38. #else
  39.         Console.WriteLine("Thread Relative Statics Sample  WITHOUT \"[ThreadStaticAttribute( )]\"");
  40. #endif
  41.         ThdRelStaticSample    mainThd    = new ThdRelStaticSample( );
  42.  
  43.         lock(mainThd.E1)                    //    Monitor.Enter on the same object that mainThd.thd1
  44.         {                                    //        and mainThd.thd2 using for coordinating
  45.             Console.Write("mainThd.E1  ");    //        Console output on
  46.             mainThd.E1.DebugOut( );
  47.         }                                    //    Monitor.Exit(mainThd.E1);
  48.  
  49.         mainThd.E1.Change( );                //    Adds E.jumpChange to E.stat
  50.  
  51.         lock(mainThd.E1)                    //    Monitor.Enter on the same object that mainThd.thd1
  52.         {                                    //        and mainThd.thd2 using for cooridinating
  53.             Console.Write("mainThd.E2  ");    //        Console output on
  54.             mainThd.E2.DebugOut( );
  55.         }                                    //    Monitor.Exit(mainThd.E1);
  56.  
  57.         mainThd.E2.Change( );                //    Adds E.jumpChange to E.stat
  58.  
  59.         mainThd.thd1.Join( );                //    Join on mainThd.thd1, no Stop.  Thread to run to completion
  60.         mainThd.thd2.Stop( );                //    Stop on mainThd.thd2.
  61.         mainThd.thd2.Join( );                //    Join on mainThd.thd2.  (Now both of these threads are dead)
  62.  
  63.         Console.Write("mainThd.E1  ");        //    mainThd.thd1 and mainThd.thd2 are dead, so we
  64.         mainThd.E1.DebugOut( );                //        don't need to coordinate Console with anyone.
  65.         
  66.         mainThd.E1.Change( );                //    Adds E.jumpChange to E.stat
  67.  
  68.         Console.Write("mainThd.E2  ");        //    mainThd.thd1 and mainThd.thd2 are dead, so we
  69.         mainThd.E2.DebugOut( );                //        don't need to coordinate Console with anyone.
  70.     }
  71. }
  72.  
  73. public class E
  74. {
  75.                                             //    "[ThreadStaticAttribute( )]" is an attribute, much like "public" and/or "static"
  76.                                             //    In the next few lines, the "[ThreadStaticAttribute( )]" is applied to the
  77.                                             //        field E.stat if this code is compiled without the "/D:WITHOUT"
  78.                                             //        switch on the CSharp compiler.
  79. #if        !WITHOUT
  80.     [ThreadStaticAttribute( )]
  81. #endif
  82.         public static int    stat;
  83.     public static int    startHere            = 3;
  84.     public static int    jumpChange            = 2;
  85.     public static int    loopRun1First        = 10000;
  86.     public static int    loopRun1Second        = 20000;
  87.     public static int    loopRun2First        = -5000;
  88.     public static int    resetRun2First        = 1000;
  89.     public static int    resetRun2Finally    = 2001;
  90.     public E( )
  91.     {
  92.         stat = startHere;
  93.     }
  94.  
  95.     public void Run1( )
  96.     {
  97.         while(stat < loopRun1First)
  98.             stat++;
  99.  
  100.         lock(this)                                //    Monitor.Enter(this)
  101.         {
  102.             Console.Write("   Run1 L1  ");        //    loopRun1First, Loop One.
  103.             DebugOut( );
  104.         }                                        //    Monitor.Exit(this)
  105.  
  106.         while(stat < loopRun1Second)
  107.             stat++;
  108.  
  109.         lock(this)                                //    Monitor.Enter(this)
  110.         {
  111.             Console.Write("   Run1 L2  ");        //    loopRun1Second, Loop Two.
  112.             DebugOut( );
  113.         }                                        //    Monitor.Exit(this)
  114.     }
  115.  
  116.     public void Run2( )
  117.     {
  118.         try
  119.         {
  120.             while(true)
  121.             {
  122.                 stat--;
  123.                 if (stat == loopRun2First)
  124.                 {
  125.                     lock(this)                    //    Monitor.Enter(this)
  126.                     {
  127.                         Console.Write("   Run2     ");
  128.                         DebugOut( );
  129.                     }                            //    Monitor.Exit(this)
  130.                     stat = resetRun2First;
  131.                 }
  132.             }
  133.         }
  134.         finally
  135.         {
  136.             lock(this)                            //    Monitor.Enter(this) and Monitor.Exit(this)
  137.                 DebugOut( );                    //        as a wrapper around this single statement.
  138.             stat = resetRun2Finally;
  139.             lock(this)                            //    Monitor.Enter(this)
  140.             {
  141.                 Console.Write("   Run2 fin ");            //    Run2 is in the finally block
  142.                 DebugOut( );
  143.             }                                    //    Monitor.Exit(this)
  144.         }
  145.     }
  146.  
  147.     public void Change( )
  148.     {
  149.         stat+=jumpChange;
  150.     }
  151.     public void DebugOut( )
  152.     {
  153.         Console.WriteLine("stat = {0}",stat);
  154.     }
  155.  
  156. }
  157.  
  158. /*
  159.  
  160.   Output of TRSS.exe
  161.  
  162. Thread Relative Statics Sample  WITHOUT "[ThreadStaticAttribute( )]"
  163. mainThd.E1  stat = 3
  164.    Run2     stat = -5000
  165.    Run1 L1  stat = -5000
  166.    Run2     stat = 20000
  167.    Run1 L2  stat = -5000
  168.    Run2     stat = -5000
  169.    Run2     stat = -5000
  170.    Run2     stat = -5000
  171.    Run2     stat = -5000
  172. mainThd.E2  stat = -4998
  173.    Run2     stat = -5000
  174.    Run2 fin stat = 2001
  175. mainThd.E1  stat = 2001
  176. mainThd.E2  stat = 2003
  177.  
  178.   ======================================================================================
  179.   Output of ThdRelStaticSample.exe
  180.  
  181. Thread Relative Statics Sample  with "[ThreadStaticAttribute( )]"
  182. mainThd.E1  stat = 3
  183. mainThd.E2  stat = 5
  184.    Run2     stat = -5000
  185.    Run1 L1  stat = 10000
  186.    Run2     stat = -5000
  187.    Run2     stat = -5000
  188.    Run1 L2  stat = 20000
  189.    Run2     stat = -5000
  190.    Run2 fin stat = 2001
  191. mainThd.E1  stat = 7
  192. mainThd.E2  stat = 9
  193.  
  194. */
  195.