C# Timer and StopWatch Classes

Timer Class

The Timer will generate an event after a set interval. There is an option to generate a sequence of events instead of just one.

This is in the System.Timers namespace.

Constructors

Properties (some of them)

Methods (some of them)

Events (one of them)

The Elapsed event occurs when the appropriate number of milliseconds has passed.

     public event ElapsedEventHandler Elapsed

The second argument for the event handler will be of type ElapsedEventArgs, which has one property (get only):

     public DateTime SignalTime

giving us the date and time of the event.

Here ElapsedEventHandler is:

     public delegate void ElapsedEventHandler(object sender, ElapsedEventArgs e)


Stopwatch Class

The StopWatch class is intended to allow us to measure elapsed time accurately.

It is in the System.Diagnostics namespace.

In a typical Stopwatch scenario, we call the Start method, do something, call the Stop method, and then check elapsed time using the Elapsed property.

Constructor

     public StopWatch()

This creates a Stopwatch object. Initially it is stopped, and the elapsed time property is 0.

Properties (some of them)

Methods (some of them)