Home > DeveloperSection > Articles > Timer in C Sharp

Timer in C Sharp


C# C# 
Ratings:
0 Comment(s)
 4281  View(s)
Rate this:

Timer in C#.Net


The Timer control allows you to set a time interval to peridically execute an event at a specified interval. It is useful when you want to execute certain applications after a certain interval. Say you want to create a hourly backup of your data. You can make a routine that will take the backup and call that routine in the Timer's event and set the timer interval for an hour.

Using timer control is very simple. To demonstrate this I am going to make an application in which 5 seconds after button click a message box will be displayed “Timer working”.

To use timer control drag and drop timer control from tool box to the form.

Coding

 

Code at button click event.

private void btnClick_Click(object sender, EventArgs e)

        {

//setting interval of timer to 5000 miliseconds.

//1000 miliseconds = 1 sec

//so 5000miliseconds = 5 sec.

            timer1.Interval = 5000;

//starting timer.

            timer1.Enabled = true;

        }

 

//code for timer tick event

        private void timer1_Tick(object sender, EventArgs e)

        {

//displaying message box.

            MessageBox.Show("Timer Working");

//stoping timer.

            timer1.Enabled = false;           

        }

Screen shot

Member of timer

Tick

This event occurs when the Interval has elapsed. 

Start

Starts raising the Tick event by setting Enabled to true. 

Stop

Stops raising the Tick event by setting Enabled to false. 

Close

Releases the resources used by the Timer. 

AutoReset

Indicates whether the Timer raises the Tick event each time the specified Interval has elapsed or whether the Tick event is raised only once after the first interval has elapsed.

Interval

Indicates the interval on which to raise the Tick event.

Enabled

Indicates whether the Timer raises the Tick event.

 


Don't want to miss updates? Please click the below button!

Follow MindStick