March 2004
Intermediate to advanced
560 pages
26h 47m
English
using System;
using System.Threading;
namespace Samples
{
public class TimerCallbackSample
{
public static AutoResetEvent are =
new AutoResetEvent(false);
public static Timer t = null;
public const int number = 9;
public class StateHolder
{
public int counter = 0;
}
static void CallMe(Object s)
{
StateHolder sh = (StateHolder) s;
Console.WriteLine("{0}: {1}",
sh.counter++, DateTime.Now);
if(sh.counter > number)
{
t.Dispose(are);
}
}
public static void Main()
{
StateHolder sh = new StateHolder();
TimerCallback tcb =
new TimerCallback(CallMe);
t = new Timer(tcb, sh, 1000, 1000);
are.WaitOne();
}
}
}
0: 24/06/2003 3:38:29 AM 1: 24/06/2003 3:38:30 AM 2: 24/06/2003 3:38:31 AM 3: 24/06/2003 3:38:32 AM 4: 24/06/2003 3:38:33 AM ...
Read now
Unlock full access