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

  1. /*============================================================
  2. **
  3. **    Class:        SimplePool
  4. **
  5. **    Purpose:    Simple Thread Pool Sample
  6. **
  7. **    Copyright (c) Microsoft, 1999-2000
  8. **
  9. ===========================================================*/
  10.  
  11. using System;
  12. using System.Threading;
  13.  
  14. public class SomeState
  15. {
  16.     public int                    Cookie;
  17.     public SomeState(int iCookie)
  18.     {
  19.         Cookie = iCookie;
  20.     }
  21. };
  22.  
  23. public class Alpha
  24. {
  25.     public int []                HashCount;
  26.     public ManualResetEvent        eventX;
  27.     public static int            iCount        = 0;
  28.     public static int            iMaxCount    = 0;
  29.     public Alpha(int MaxCount) 
  30.     {
  31.         HashCount = new int[30];
  32.         iMaxCount = MaxCount;
  33.     }
  34.  
  35.     //    The method that will be called when the Work Item is serviced
  36.     //    on the Thread Pool
  37.     public void Beta(Object state)
  38.     {
  39.         Console.WriteLine(" {0} {1} :", Thread.CurrentThread.GetHashCode(), ((SomeState)state).Cookie);
  40.         Interlocked.Increment(ref HashCount[Thread.CurrentThread.GetHashCode()]);
  41.  
  42.         //    Do some busy work
  43.         int        iX    = 10000;
  44.         while (iX > 0)
  45.         {
  46.             iX--;
  47.         }
  48.         Interlocked.Increment(ref iCount);
  49.         if (iCount == iMaxCount)
  50.         {
  51.             Console.WriteLine("");
  52.             Console.WriteLine("Setting EventX ");
  53.             eventX.Set();
  54.         }
  55.     }
  56. };
  57.  
  58. public class SimplePool
  59. {
  60.     public static int Main(String[] args)
  61.     {
  62.         Console.WriteLine("Thread Simple Thread Pool Sample");
  63.         bool                W2K            = false;
  64.         int                    MaxCount    = 1000;
  65.         ManualResetEvent    eventX        = new ManualResetEvent(false);
  66.         Console.WriteLine("Queuing {0} items to Thread Pool", MaxCount);
  67.         Alpha                oAlpha        = new Alpha(MaxCount);
  68.         oAlpha.eventX = eventX;
  69.         Console.WriteLine("Queue to Thread Pool 0");
  70.         try
  71.         {
  72.             ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta),new SomeState(0));
  73.             W2K = true;
  74.         }
  75.         catch (NotSupportedException)
  76.         {
  77.             Console.WriteLine("These API's may fail when called on a non-Windows 2000 system.");
  78.         }
  79.         if (W2K)
  80.         {
  81.             for (int iItem=1;iItem < MaxCount;iItem++)
  82.             {
  83.                 Console.WriteLine("Queue to Thread Pool {0}", iItem);
  84.                 ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta),new SomeState(iItem));
  85.             }
  86.             Console.WriteLine("Waiting for Thread Pool to drain");
  87.             eventX.WaitOne(Timeout.Infinite,true);
  88.             Console.WriteLine("Thread Pool has been drained (Event fired)");
  89.             Console.WriteLine("");
  90.             Console.WriteLine("Load across threads");
  91.             for(int iIndex=0;iIndex<oAlpha.HashCount.Length;iIndex++)
  92.                 Console.WriteLine("{0} {1}", iIndex, oAlpha.HashCount[iIndex]);
  93.         }
  94.         return 0;
  95.     }
  96. }
  97.  
  98.