articles

Home / DeveloperSection / Articles / Timer in C Sharp

Timer in C Sharp

Anonymous User7322 31-Jul-2010

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.

Timer in C Sharp

Coding 

Code at button click event.

privatevoid 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
        privatevoid timer1_Tick(object sender, EventArgs e)
        {
//displaying message box.
            MessageBox.Show("Timer Working");
//stoping timer.
            timer1.Enabled = false;           
        }
Screen shot

Timer in C Sharp

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.

 


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By