Timers Timers are used in programs for many different purposes. Measuring Algorithm Speeds To measure the execution speed of an algorithm you need to use timer objects as a very fast stopwatch. Sample code for determining the execution speed of an algorithm using the DateTime object. The DateTime object has many different properties. In the code above we are using the Millisecond property to measure the execution time of the loop. For faster algorithms you could use the Ticks property instead as it will return a value in units of one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second. Date Arithmetic Here we are using the DateTime.Today object and the DateTime() constructor to determine how many days a person has been living. Timers Timers are typically used in graphical applications to perform animation or perform periodic tasks in your application. A Timer is used to raise an event at user-defined intervals. Whenever the Enabled property is set to true and the Interval property is greater than zero, the Tick event is raised at intervals based on the Interval property setting. Let's assume we want to create a Timer that fires once per second to move an pictureBox object around the window. Here we have defined the interval of the Timer at 1000 milliseconds or 1 second. We have set the timer object (tm) properties of Interval and Tick. The Tick property is an EventHandler object referring to a method or function called tickHandler. Finally, we start or enable the Timer. Now the tickHandler function will be called once per second unless the timer is disabled or the Interval changes. Here is an example of an event handler method. This method will generate a new random X and Y value (these are global variables) and then use these values to create a new Point() object to relocate a pictureBox control known as pbTarget. The Location property of the pictureBox can be updated to move the object on the Window – therefore producing an animation. Snow Maker Example public partial class Form1 : Form { int X = 0; int Y = 0; int interval = 100; // Snow speed Timer tm = new Timer(); Random rand = new Random(); public Form1() { InitializeComponent(); tm.Interval = interval; tm.Tick += new EventHandler(snowMaker); } void snowMaker(object sender, EventArgs e) { SolidBrush myBrush = new SolidBrush(Color.White); // White snow X = ((int)(rand.Next(0, this.Width))); // Random horizontal position Y = ((int)(rand.Next(0, this.Height))); // Random vertical position Graphics formGraphics = this.CreateGraphics(); formGraphics.FillEllipse(myBrush, new Rectangle(X, Y, 4, 4)); // top left (size) myBrush.Dispose(); formGraphics.Dispose(); } private void btnStart_Click(object sender, EventArgs e) { tm.Start(); } private void btnStop_Click(object sender, EventArgs e) { tm.Stop(); } private void btnReset_Click(object sender, EventArgs e) { this.Invalidate(); // Ensures the form is repainted this.Update(); } } Exercises 1. Create a console application to test the execution time of an algorithm to compute the positive integer factors of a user provided number. Use the algorithm shown below, but add a millisecond Timer object and display the execution time for various values. Record your findings for the time (in milliseconds or seconds) to factor relatively large numbers. 2. Create the graphical snowing application with the 3 buttons. Use a Timer to control the speed of the snowfall.