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

  1. /*    ==========================================================    *\
  2.     Class:    CS_Timers
  3.     Copyright (c) Microsoft, 1999-2000
  4. \*    ==========================================================    */
  5. using System;
  6. using System.Threading;
  7.  
  8. public class Alpha
  9. {
  10.     private ManualResetEvent    e;
  11.     public Alpha(ManualResetEvent ev)
  12.     {
  13.         e = ev;
  14.     }
  15.     public void Alpha2(Object o)
  16.     {
  17.         Console.WriteLine("Alpha2");
  18.     }
  19.     public void Alpha3(Object o)
  20.     {
  21.         Console.WriteLine("Alpha3");
  22.     }
  23.     public void Alpha4(Object o)
  24.     {
  25.         Console.WriteLine("Alpha4");
  26.     }
  27.     public void Alpha1(Object o)
  28.     {
  29.         Console.WriteLine("Alpha1");
  30.         e.Set( );
  31.     }
  32. }
  33.  
  34. public class TimersSample
  35. {
  36.     public TimersSample( )
  37.     {
  38.     }
  39.     public void test_timer( )
  40.     {
  41.         ManualResetEvent    ev            = new ManualResetEvent(false);
  42.         Alpha                alpha        = new Alpha(ev);
  43.         bool                w2k            = true;
  44.         try
  45.         {
  46.             Timer        testX    = new Timer(new TimerCallback(alpha.Alpha1),this,100,0);
  47.             testX.Dispose(ev);
  48.         }
  49.         catch (NotSupportedException)
  50.         {
  51.             Console.WriteLine("NotSupportedException was caught because: ");
  52.             Console.WriteLine("\tSystem.Timer Not Supported on this system.");
  53.             Console.WriteLine("\tMust be running on Windows 2000, or the COM+ Win32 Support");
  54.             w2k = false;
  55.         }
  56.         if (w2k)
  57.         {
  58.             int                    iF            = 4000;
  59.             int                    iP            = 0;
  60.             int                    uiF            = 2000;
  61.             int                    uiP            = 1000;
  62.             long                lF            = 3000L;
  63.             long                lP            = 500L;
  64.             long                lFc            = 4000L;
  65.             long                lPc            = 2000L;
  66.             ev.Reset( );
  67.             Console.WriteLine("Entering wait for 1 second");
  68.             ev.WaitOne(1000,false);
  69.             Console.WriteLine("Time expired");
  70.             TimerCallback        timer_cb_1    = new TimerCallback(alpha.Alpha1);
  71.             TimerCallback        timer_cb_2    = new TimerCallback(alpha.Alpha2);
  72.             TimerCallback        timer_cb_3    = new TimerCallback(alpha.Alpha3);
  73.             TimerCallback        timer_cb_4    = new TimerCallback(alpha.Alpha4);
  74.             Timer                t1            = new Timer(timer_cb_1,this,iF,iP);
  75.             Timer                t2            = new Timer(timer_cb_2,this,(UInt32)uiF,(UInt32)uiP);
  76.             Timer                t3            = new Timer(timer_cb_3,this,lF,lP);
  77.             ev.WaitOne( );
  78.             Console.WriteLine("t1 fired");
  79.             t2.Change(lFc,lPc);
  80.             ev.Reset( );
  81.             bool    flag;
  82.             try
  83.             {
  84.                 flag = t3.Dispose(ev);
  85.             }
  86.             catch (ApplicationException)
  87.             {
  88.                 Console.WriteLine("Caught an informational ApplicationException  t3.Dispose(ev)");
  89.             }
  90.             ev.WaitOne( );
  91.             t3    = new Timer(timer_cb_4,this,1000,100);
  92.             t2.Change((UInt32)uiF,(UInt32)uiP);
  93.             ev.Reset();
  94.             Console.WriteLine("Entering wait for 3 seconds");
  95.             ev.WaitOne(3000,false);
  96.             t2.Change(iF,iP);
  97.             ev.Reset();
  98.             Console.WriteLine("Entering wait for 3 seconds, again");
  99.             ev.WaitOne(3000,false);
  100.             Console.WriteLine("time expired");
  101.             try
  102.             {
  103.                 t1.Dispose(ev);
  104.             }
  105.             catch (ApplicationException)
  106.             {
  107.                 Console.WriteLine("Caught an informational ApplicationException  t1.Dispose(ev)");
  108.             }
  109.             try
  110.             {
  111.                 t2.Dispose(ev);
  112.             }
  113.             catch (ApplicationException)
  114.             {
  115.                 Console.WriteLine("Caught an informational ApplicationException  t2.Dispose(ev)");
  116.             }
  117.             try
  118.             {
  119.                 t3.Dispose( );
  120.             }
  121.             catch (ApplicationException)
  122.             {
  123.                 Console.WriteLine("Caught an informational ApplicationException  t3.Dispose( )");
  124.             }
  125.             ev.WaitOne( );
  126.             Console.WriteLine("Timers are deleted");
  127.         }
  128.         Console.WriteLine("#########");
  129.     }
  130.  
  131.     public static void Main(String[] args)
  132.     {
  133.         Console.WriteLine("TimersSample.cs");
  134.         Console.WriteLine("Run on Build {0}",Environment.Version);
  135.         TimersSample    X    = new TimersSample( );
  136.         X.test_timer( );
  137.     }
  138. }
  139.