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

  1. using System;
  2. using System.Threading;
  3. using System.Runtime.Remoting;
  4.  
  5. // Async delegate
  6. public delegate bool FactorizingCallback(
  7.                                         int factorizableNum, 
  8.                                         ref int primefactor1,
  9.                                         ref int primefactor2);
  10.  
  11. // Class the factorizers the number
  12. public class PrimeFactorizer
  13. {
  14.     public bool Factorize(
  15.                          int factorizableNum,  
  16.                          ref int primefactor1,
  17.                          ref int primefactor2)
  18.     {
  19.         primefactor1 = 1;
  20.         primefactor2 = factorizableNum;
  21.  
  22.         // Factorize using a low tech approach
  23.         for (int i=2;i<factorizableNum;i++)
  24.         {
  25.             if (0 == (factorizableNum % i))
  26.             {
  27.                 primefactor1 = i;
  28.                 primefactor2 = factorizableNum / i;
  29.                 break;
  30.             }
  31.         }
  32.  
  33.         if (1 == primefactor1 )
  34.             return false;
  35.         else
  36.             return true ;
  37.     }
  38. }
  39.  
  40. // Class that receives a callback when the the results are available
  41. public class ProcessFactorizedNumber
  42. {
  43.     private int _ulNumber;
  44.  
  45.     public ProcessFactorizedNumber(int number)
  46.     {
  47.         _ulNumber = number;
  48.     }
  49.  
  50.     // Note the qualifier one-way.
  51.     [OneWayAttribute()]
  52.     public void FactorizedResults(IAsyncResult ar)
  53.     {
  54.         int factor1=0, factor2=0; 
  55.  
  56.         // Extract the delegate from the AsyncResult  
  57.         FactorizingCallback fd = (FactorizingCallback)ar.AsyncObject;
  58.  
  59.         // Obtain the result
  60.         fd.EndInvoke(ref factor1, ref factor2, ar);
  61.  
  62.         // Output results
  63.         Console.WriteLine("On CallBack: Factors of {0} : {1} {2}", 
  64.                           _ulNumber, factor1, factor2);
  65.     }
  66. }
  67.  
  68. // Class that shows variations of using async
  69. public class Simple 
  70. {
  71.     // Async Variation 1
  72.     // The ProcessFactorizedNumber.FactorizedResults callback 
  73.     // is called when the call completes.
  74.     public void FactorizeNumber1()
  75.     {
  76.         // Client code
  77.         PrimeFactorizer pf = new PrimeFactorizer();
  78.         FactorizingCallback fd = new FactorizingCallback(pf.Factorize);
  79.  
  80.         // Async Variation 1
  81.         int factorizableNum = 1000589023, temp=0; 
  82.  
  83.         // Create an instance of the class which is going 
  84.         // to called when the call completes
  85.         ProcessFactorizedNumber fc = new ProcessFactorizedNumber(factorizableNum);
  86.  
  87.         // Define the AsyncCallback delegate
  88.         AsyncCallback cb = new AsyncCallback(fc.FactorizedResults);
  89.  
  90.         // Can stuff any object as the state object
  91.         Object state = new Object();
  92.  
  93.         // Asynchronously invoke the Factorize method on pf
  94.         IAsyncResult ar = fd.BeginInvoke(
  95.                                         factorizableNum, 
  96.                                         ref temp, 
  97.                                         ref temp, 
  98.                                         cb, 
  99.                                         state); 
  100.  
  101.         //
  102.         // Do some other useful work
  103.         //. . .
  104.     }
  105.  
  106.     // Async Variation 2
  107.     // Waits for the result
  108.     public void FactorizeNumber2()
  109.     {
  110.         // Client code
  111.         PrimeFactorizer pf = new PrimeFactorizer();
  112.         FactorizingCallback fd = new FactorizingCallback(pf.Factorize);
  113.  
  114.         // Async Variation 1
  115.         int factorizableNum = 1000589023, temp=0; 
  116.  
  117.         // Create an instance of the class which is going 
  118.         // to called when the call completes
  119.         ProcessFactorizedNumber fc = new ProcessFactorizedNumber(factorizableNum);
  120.  
  121.         // Define the AsyncCallback delegate
  122.         AsyncCallback cb = 
  123.         new AsyncCallback(fc.FactorizedResults);
  124.  
  125.         // Can stuff any object as the state object
  126.         Object state = new Object();
  127.  
  128.         // Asynchronously invoke the Factorize method on pf
  129.         IAsyncResult ar = fd.BeginInvoke(
  130.                                         factorizableNum, 
  131.                                         ref temp, 
  132.                                         ref temp, 
  133.                                         null, 
  134.                                         null); 
  135.  
  136.         ar.AsyncWaitHandle.WaitOne(10000, false);
  137.  
  138.         if (ar.IsCompleted)
  139.         {
  140.             int factor1=0, factor2=0; 
  141.  
  142.             // Obtain the result
  143.             fd.EndInvoke(ref factor1, ref factor2, ar);
  144.  
  145.             // Output results
  146.  
  147.             Console.WriteLine("Sequencial : Factors of {0} : {1} {2}", 
  148.                               factorizableNum, factor1, factor2);
  149.  
  150.         }
  151.     }
  152.  
  153.  
  154.     public static void Main(String[] args)
  155.     {
  156.         Simple simple = new Simple();
  157.         simple.FactorizeNumber1();
  158.         simple.FactorizeNumber2();
  159.     }
  160. }
  161.  
  162.  
  163.  
  164.