Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Topics

Recent Status Updates

View All Updates

* * * * * 1 votes

C# - Working With Timers

timer countdown

  • Please log in to reply
1 reply to this topic

#1 CommittedC0der


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 26 August 2011 - 11:22 AM

Hello everyone. Today we'll be going over Timers in C#, I hope you enjoy this tutorial.

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, this 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]

timer.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]

timer.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)
   counter--; [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!");
      counter = timer.Interval;[COLOR=#006400]//Reset the counter.[/COLOR]
      timer.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!");
timer.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.

Thanks ~ Committed. :)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#2 AhmedFaid


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:C#, (Visual) Basic, Visual Basic .NET

Posted 07 April 2014 - 05:05 AM

thank 4 this wonderful topic , i was looking to make a video or a slideshow of picture ( as a screen saver ) for my program, so i'll use a timer to run this screen saver after 10 min.

my idea is that i'll make the timer reset each time the user do any action, but after 10 min without actions the screen saver runs, and then after clicking the the video; it stops and hide and then the program return as it is.


sorry for the long replay , but i'm happy for your help, and if u have any other idea of doing what i need please replay

thanks again :)

  • 0

Also tagged with one or more of these keywords: timer, countdown

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download