Home > DeveloperSection > Articles > Creating Timer at Runtime in C Sharp .NET

Creating Timer at Runtime in C Sharp .NET


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

Creating Timer at Runtime in C#.Net


Here I’m going to create timer at runtime. I’ll set timer’s interval to 5seconds=5000miliseconds. And after 5sec back color of form will be changed to blue.

Code

//creating timer

  Timer tm = new Timer();

//at button click event setting timer interval to 5000 and enabling the timer

       

        private void btnClick_Click(object sender, EventArgs e)

        {

            tm.Interval = 5000;

            tm.Enabled = true;

        }

 

//writing timer event handler at tick event of timer.

        private void tm_Tick(object sender, EventArgs e)

        {

            this.BackColor = Color.Blue;

            tm.Enabled = false;

        }

 

//at form load event creating timer event handler

        private void Timer2_Load(object sender, EventArgs e)

        {

            tm.Tick += new EventHandler(tm_Tick);

        }

 

Screen shot

Back color of form changed after 5 seconds of button click


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

Follow MindStick