Information Technology Reference
In-Depth Information
The actual code is the following. The only things I haven't shown previously are the private
timer field, called MyPrivateTimer , and the constructor for the class. The constructor does the
work of setting up the internal timer and attaching it to event handler OnOneSecond .
public class MyTimerClass
{
public event EventHandler Elapsed;
private void OnOneSecond(object obj, EventArgs e)
{
if (Elapsed != null)
Elapsed(obj, e);
}
//------------
private System.Timers.Timer MyPrivateTimer; // Private timer.
public MyTimerClass() // Constructor
{
MyPrivateTimer = new System.Timers.Timer(); // Create the private timer.
// The following statement sets our OnOneSecond method above as an event
// handler to the Elapsed event of class Timer. It is completely
// unrelated to our event Elapsed, declared above.
MyPrivateTimer.Elapsed += OnOneSecond; // Attach our event handler.
// Property Interval is of type double, and specifies the number of
// milliseconds between when its event is raised.
MyPrivateTimer.Interval = 1000; // 1 second interval.
// Property Enabled is of type bool, and turns the timer on and off.
MyPrivateTimer.Enabled = true; // Start the timer.
}
}
Search WWH ::




Custom Search