C# - Working With Timers

Declaring a Timer.  
Before we get started we have two things that will determine how our timer works later.  If you want to add an event that lets us know when our timer is done we must use the "Timers" class.  If you just wish to implement a variable that must be checked as the timer counts down to let us know when it hits 0, we can use a normal timer.  The choice is up to you, but we'll cover both ways in this tutorial.

If you chose "Timers" then we must add the "Timers" namespace to our project.
(If you chose the other way, or if you dropped a timer on the Form from the tool box, skip this line.)
using System.Timers;

Now to declare a new timer we first create a new instance of a timer(or a timer from the namespace "Timers"), like so.
Timer timer = new Timer();[COLOR=#006400] //Normal Timer.[/COLOR]
System.Timers.Timer timer = new System.Timers.Timer();[COLOR=#006400]//From Timers namespace.[/COLOR]

From now on both timers code will be the same until the end of the next section.

With out newly created timer(s) we can assign the time through the Interval property.
timer.Interval = 5000;
(Its important to note that Timers ONLY count down, they do NOT count up.)

You may notice I set the timers Interval to 5000, that is because timers use milliseconds, meaning this timer will run for 5 seconds before its done. The ratio is: 1000 millisecond = 1 second.   An easy way to avoid messing up your time is to just multiply by 1000.
timer.Interval = 5*1000;
Then you just change '5' to the number of seconds you wish to use and its converted for you. :)

Timer Actions.
Now that we have a timer lets learn how to control it!  

The first thing is starting the timer, thi
s can be done with the .Start() method.

timer.Start();[COLOR=#006400]//It will begin counting down from its interval.[/COLOR]

Stopping the timer can be done likewise with the .Stop() method.
timer.Stop();[COLOR=#006400]//This is just like a DVD player, stop resets the countdown back to its starting point.[/COLOR]

Pausing on the other hand is done a little bit differently, but don't worry its still easy! To pause a timer we must unable it, then to un-pause it we must re-enable it.
timer.Enabled = false;[COLOR=#006400]//Timer paused.[/COLOR]

.Enabled = true; [COLOR=#006400]//Timer un-paused.[/COLOR]

The last action is only available if you chose the "Timers" namespace, this is the property of AutoReset.  As you may of guess this just automatically resets the timer once it hits 0.
timer.AutoReset = true; [COLOR=#006400]//It will automatically reset once it hits 0.[/COLOR]

.AutoReset = false;[COLOR=#006400] //You must call the .Start() method to restart the timer.[/COLOR]

Checking the Time.
Now this is where the difference in timers will show through.  Each has it benefits, but don't worry there isn't a wrong choice.

The first way we'll go over is using the Timer_Tick event which comes with the default timer.

To check the time with this event we must assign a counter, like so.
double counter = timer.Interval;

Then in our Timer_Tick event we can check this counter.
private void timer1_Tick_1(object sender, EventArgs e)
--; [COLOR=#006400]//Decrease the counter, just like the timer decreases.[/COLOR]
if (counter == 0) [COLOR=#006400]//If we hit 0, or any other number we'd like to check.[/COLOR]

MessageBox.Show("Time Up!");
= timer.Interval;[COLOR=#006400]//Reset the counter.[/COLOR]
.Start();[COLOR=#006400]//Re-start the timer.[/COLOR]
}               }

Now for our second timer, the one created through the "Timers" namespace.  This timer has a little bit different event, its event tells us when the timer hits 0, but we cannot check the time in between.(We cant check the time at any tick events like above.)

This event requires a little code to create first, that is adding a new event handler and event function.
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);[COLOR=#006400]//Handler, this goes inside a button click for instance. [/COLOR]
private void timer_Elapsed(object sender, ElapsedEventArgs e)[COLOR=#006400]//Function, this goes out side with other events.[/COLOR]
MessageBox.Show("Time Up!");
.Start();[COLOR=#006400]//re-start if AutoReset is off.[/COLOR]

And there you have it! How to work with Timers in C#!  I hope you learned how to use timers affectingly in your project.  As always questions, comments, and +rep welcome, also don't forget to use the "Rate this Thread" button at the top of this post.

No comments:

Post a Comment