home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Remoting / asyncdelegate / asyncdelegate2.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  3.4 KB  |  115 lines

  1. using System;
  2. using System.Threading;
  3. using System.Runtime.Remoting;
  4.  
  5. //
  6. // Context-Bound type with Synchronization Context Attribute
  7. //
  8. [Synchronization()]
  9. public class Wak : ContextBoundObject
  10. {
  11.     //
  12.     // A method that does some work
  13.     //
  14.     public int Pat(int i)
  15.     {
  16.         Console.WriteLine("Hash: {0} Wak Pat", Thread.CurrentThread.GetHashCode());
  17.         return i*2;
  18.     }
  19. };
  20.  
  21. //
  22. // Async delegate
  23. //
  24. public delegate  int WakPatDelegate(int i);
  25.  
  26. public class Simple 
  27. {
  28.     //
  29.     // Asynchronous Callback method
  30.     //
  31.     public static void SomeMethod(IAsyncResult ar)
  32.     {
  33.         // Obtain value from AsyncState object
  34.         int value = Convert.ToInt32(ar.AsyncState);
  35.  
  36.         // Obtain results via EndInvoke
  37.         int result = ((WakPatDelegate)ar.AsyncObject).EndInvoke(ar);
  38.  
  39.         Console.WriteLine("Simple.SomeMethod (AsyncCallback): Result of {0} in Wak.Pak is {1} ",value, result);
  40.     }
  41.  
  42.     public static void Main(String[] args)
  43.     {
  44.         Console.WriteLine("Thread Simple Context Sample");
  45.         Console.WriteLine("");
  46.  
  47.         Console.WriteLine("Make an instance of a context-bound type Wak");
  48.         Wak oWak = new Wak();
  49.  
  50.         if (RemotingServices.IsTransparentProxy(oWak))
  51.             Console.WriteLine("oWak Is a Proxy");
  52.         else
  53.             Console.WriteLine("oWak Is NOT a Proxy");
  54.  
  55.         int value=0;
  56.         int result=0;
  57.  
  58.         Console.WriteLine("Make a sync call on the object");
  59.         value = 10;
  60.         result = oWak.Pat(value);
  61.         Console.WriteLine("Result of {0} in Wak.Pak is {1} ",value, result);
  62.  
  63.         Console.WriteLine("Make single Async call on Context-bound object");
  64.         WakPatDelegate wpD1 = new WakPatDelegate(oWak.Pat);
  65.         value = 20;
  66.         IAsyncResult ar1 = wpD1.BeginInvoke(value,null,null);
  67.  
  68.         //
  69.         //Wait for the call to complete
  70.         //
  71.         ar1.AsyncWaitHandle.WaitOne();
  72.         result = wpD1.EndInvoke(ar1);
  73.         Console.WriteLine("Result of {0} in Wak.Pak is {1} ",value, result);
  74.  
  75.         Console.WriteLine("Make single Async call on Context-bound object - use AsyncCallback and StateObject");
  76.         WakPatDelegate wpD2 = new WakPatDelegate(oWak.Pat);
  77.         value = 30;
  78.         IAsyncResult ar2 = wpD2.BeginInvoke(
  79.                                            value,
  80.                                            new AsyncCallback(Simple.SomeMethod),
  81.                                            value
  82.                                            );
  83.  
  84.         Console.WriteLine("Make multiple Async calls on Context-bound object");
  85.         int asyncCalls = 5;
  86.         IAsyncResult[] ars = new IAsyncResult[asyncCalls];
  87.         WaitHandle[] whs = new WaitHandle[asyncCalls];
  88.         int[] values = new int[asyncCalls];
  89.         WakPatDelegate wpD3 = new WakPatDelegate(oWak.Pat);
  90.  
  91.         for (int i=0; i < asyncCalls; i++)
  92.         {
  93.             values[i] = i;
  94.             ars[i] = wpD3.BeginInvoke(values[i],null,null);
  95.             whs[i] = ars[i].AsyncWaitHandle;
  96.         }
  97.  
  98.         // Give them 1000ms to complete
  99.         WaitHandle.WaitAll(whs,1000, false);
  100.  
  101.         for (int i=0; i < asyncCalls; i++)
  102.         {
  103.             result = wpD3.EndInvoke(ars[i]);
  104.             Console.WriteLine("Result of {0} in Wak.Pak is {1} ",values[i], result);
  105.         }
  106.  
  107.         Console.WriteLine("");
  108.         Console.WriteLine("Done");
  109.     }
  110. }
  111.  
  112.  
  113.  
  114.  
  115.