home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Remoting / Contexts / simple.cs < prev   
Encoding:
Text File  |  2000-06-23  |  2.2 KB  |  100 lines

  1. using System;
  2. using System.Threading;
  3. using System.Runtime.Remoting;
  4.  
  5.  
  6. //[Synchronization()]
  7. //[Synchronization(int flag, bool reEntrant)]
  8. //[Synchronization(int flag)]
  9. //[Synchronization(bool reEntrant)]
  10.  
  11. //[Synchronization(Synchronization.NOT_SUPPORTED)]
  12. //[Synchronization(Synchronization.SUPPORTED)]
  13. //[Synchronization(Synchronization.REQUIRED)]
  14. //[Synchronization(Synchronization.REQUIRES_NEW)]
  15.  
  16. [Synchronization()]
  17. public class Foo : ContextBoundObject 
  18. {
  19.     int count=0;
  20.  
  21.     public Foo()
  22.     {
  23.         Console.WriteLine("Hash : {0} Foo()", Thread.CurrentThread.GetHashCode());
  24.     }
  25.  
  26.     public void Method1()
  27.     {
  28.         int localCount = count;
  29.         localCount++;
  30.         count = localCount;
  31.         Console.WriteLine("Hash : {0} Foo.Method1 : count : {1}", Thread.CurrentThread.GetHashCode(), localCount);
  32.     }
  33. }
  34.  
  35. //[ThreadAffinity(ThreadAffinity.NOT_SUPPORTED)]
  36. //[ThreadAffinity(ThreadAffinity.SUPPORTED)]
  37. //[ThreadAffinity(ThreadAffinity.REQUIRED)]
  38. //[ThreadAffinity(ThreadAffinity.REQUIRES_NEW)]
  39.  
  40. [ThreadAffinity()]
  41. public class Baz : ContextBoundObject 
  42. {
  43.     int count=0;
  44.  
  45.     public Baz()
  46.     {
  47.         Console.WriteLine("Hash : {0} Baz()", Thread.CurrentThread.GetHashCode());
  48.     }
  49.  
  50.     public void Method1()
  51.     {
  52.         int localCount = count;
  53.         localCount++;
  54.         count = localCount;
  55.  
  56.         Console.WriteLine("Hash : {0} Baz.Method1 : count : {1}", Thread.CurrentThread.GetHashCode(), localCount);
  57.     }
  58. }
  59.  
  60. public class Simple
  61. {
  62.     public static Foo foo;
  63.     public static Baz baz;
  64.  
  65.     public static void ThreadMethod()
  66.     {
  67.         for (int j=0; j < 5; j++)
  68.         {
  69.             foo.Method1();
  70.         }
  71.  
  72.         for (int k=0; k < 5; k++)
  73.         {
  74.             baz.Method1();
  75.         }
  76.  
  77.  
  78.     }
  79.  
  80.     public static int Main(String[] args)
  81.     {
  82.         Console.WriteLine("Hash : {0} Main", Thread.CurrentThread.GetHashCode());
  83.  
  84.         foo = new Foo();
  85.         foo.Method1();
  86.  
  87.         baz = new Baz();
  88.         baz.Method1();
  89.  
  90.         for (int i=0;i < 5; i++)
  91.         {
  92.             ThreadStart ts = new ThreadStart(ThreadMethod);
  93.             Thread t = new Thread(ts);
  94.             t.Start();
  95.         }
  96.  
  97.         return 0;
  98.     }
  99. }
  100.