Net-informations.com
SiteMap  | About    

C# Timer Control

What is Timer Control ?

The Timer Control plays an important role in the development of programs both Client side and Server side development as well as in Windows Services. With the Timer Control we can raise events at a specific interval of time without the interaction of another thread.

C# timer control

Use of Timer Control

We require Timer Object in many situations on our development environment. We have to use Timer Object when we want to set an interval between events, periodic checking, to start a process at a fixed time schedule, to increase or decrease the speed in an animation graphics with time schedule etc.

A Timer control does not have a visual representation and works as a component in the background.

C# timer object

How to Timer Control ?

We can control programs with Timer Control in millisecond, seconds, minutes and even in hours. The Timer Control allows us to set Intervel property in milliseconds. That is, one second is equal to 1000 milliseconds. For example, if we want to set an interval of 1 minute we set the value at Interval property as 60000, means 60x1000 .

By default the Enabled property of Timer Control is False. So before running the program we have to set the Enabled property is True , then only the Timer Control starts its function.

C# timer property

Timer example

In the following program we display the current time in a Label Control. In order to develop this program, we need a Timer Control and a Label Control. Here we set the timer interval as 1000 milliseconds, that means one second, for displaying current system time in Label control for the interval of one second.

using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { label1.Text = DateTime.Now.ToString(); } } }

Start and Stop Timer Control

The Timer control have included the Start and Stop methods for start and stop the Timer control functions.

C# timer start stop

Here we run this program only 10 seconds. In order to doing this ,in the following program we set Timer interval as 1000 (1 second) and check each seconds for stopping the Timer Control after 10 seconds.




using System;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int second = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();
            second = second + 1;
            if (second >= 10)
            {
                timer1.Stop();
                MessageBox.Show("Exiting from Timer....");
            }
        }
    }
}




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.