Example

using System;
using System.Threading;

namespace Samples
{
  public class TimerSample
  {
    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();
    }
  }
}
The output is
 0: 24/06/2003 3:26:06 AM 1: 24/06/2003 3:26:07 AM 2: 24/06/2003 3:26:08 AM 3: 24/06/2003 3:26:09 AM 4: 24/06/2003 3:26:10 AM 5: 24/06/2003 ...

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.