home *** CD-ROM | disk | FTP | other *** search
- /* ========================================================== *\
- Class: CS_Timers
- Copyright (c) Microsoft, 1999-2000
- \* ========================================================== */
- using System;
- using System.Threading;
-
- public class Alpha
- {
- private ManualResetEvent e;
- public Alpha(ManualResetEvent ev)
- {
- e = ev;
- }
- public void Alpha2(Object o)
- {
- Console.WriteLine("Alpha2");
- }
- public void Alpha3(Object o)
- {
- Console.WriteLine("Alpha3");
- }
- public void Alpha4(Object o)
- {
- Console.WriteLine("Alpha4");
- }
- public void Alpha1(Object o)
- {
- Console.WriteLine("Alpha1");
- e.Set( );
- }
- }
-
- public class TimersSample
- {
- public TimersSample( )
- {
- }
- public void test_timer( )
- {
- ManualResetEvent ev = new ManualResetEvent(false);
- Alpha alpha = new Alpha(ev);
- bool w2k = true;
- try
- {
- Timer testX = new Timer(new TimerCallback(alpha.Alpha1),this,100,0);
- testX.Dispose(ev);
- }
- catch (NotSupportedException)
- {
- Console.WriteLine("NotSupportedException was caught because: ");
- Console.WriteLine("\tSystem.Timer Not Supported on this system.");
- Console.WriteLine("\tMust be running on Windows 2000, or the COM+ Win32 Support");
- w2k = false;
- }
- if (w2k)
- {
- int iF = 4000;
- int iP = 0;
- int uiF = 2000;
- int uiP = 1000;
- long lF = 3000L;
- long lP = 500L;
- long lFc = 4000L;
- long lPc = 2000L;
- ev.Reset( );
- Console.WriteLine("Entering wait for 1 second");
- ev.WaitOne(1000,false);
- Console.WriteLine("Time expired");
- TimerCallback timer_cb_1 = new TimerCallback(alpha.Alpha1);
- TimerCallback timer_cb_2 = new TimerCallback(alpha.Alpha2);
- TimerCallback timer_cb_3 = new TimerCallback(alpha.Alpha3);
- TimerCallback timer_cb_4 = new TimerCallback(alpha.Alpha4);
- Timer t1 = new Timer(timer_cb_1,this,iF,iP);
- Timer t2 = new Timer(timer_cb_2,this,(UInt32)uiF,(UInt32)uiP);
- Timer t3 = new Timer(timer_cb_3,this,lF,lP);
- ev.WaitOne( );
- Console.WriteLine("t1 fired");
- t2.Change(lFc,lPc);
- ev.Reset( );
- bool flag;
- try
- {
- flag = t3.Dispose(ev);
- }
- catch (ApplicationException)
- {
- Console.WriteLine("Caught an informational ApplicationException t3.Dispose(ev)");
- }
- ev.WaitOne( );
- t3 = new Timer(timer_cb_4,this,1000,100);
- t2.Change((UInt32)uiF,(UInt32)uiP);
- ev.Reset();
- Console.WriteLine("Entering wait for 3 seconds");
- ev.WaitOne(3000,false);
- t2.Change(iF,iP);
- ev.Reset();
- Console.WriteLine("Entering wait for 3 seconds, again");
- ev.WaitOne(3000,false);
- Console.WriteLine("time expired");
- try
- {
- t1.Dispose(ev);
- }
- catch (ApplicationException)
- {
- Console.WriteLine("Caught an informational ApplicationException t1.Dispose(ev)");
- }
- try
- {
- t2.Dispose(ev);
- }
- catch (ApplicationException)
- {
- Console.WriteLine("Caught an informational ApplicationException t2.Dispose(ev)");
- }
- try
- {
- t3.Dispose( );
- }
- catch (ApplicationException)
- {
- Console.WriteLine("Caught an informational ApplicationException t3.Dispose( )");
- }
- ev.WaitOne( );
- Console.WriteLine("Timers are deleted");
- }
- Console.WriteLine("#########");
- }
-
- public static void Main(String[] args)
- {
- Console.WriteLine("TimersSample.cs");
- Console.WriteLine("Run on Build {0}",Environment.Version);
- TimersSample X = new TimersSample( );
- X.test_timer( );
- }
- }
-