articles

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

Creating Timer at Runtime in C Sharp .NET

Creating Timer at Runtime in C Sharp .NET

Anonymous User 29790 29-Jul-2010

Creating Timer at Runtime in C Sharp .NET

Here I’m going to create a timer at runtime.
I’ll set timer’s interval to 5seconds = 5000 milliseconds.
And after 5sec back color of the 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);
        }

 Screenshot

Creating Timer at Runtime in C Sharp .NET
The back color of form changed after 5 seconds of a button click.

You should read this Article - Dynamically loading an image in Image control in ASP.NET


c# c# 
Updated 14-Jul-2020
I am a content writter !

Leave Comment

Comments

Liked By