Timer Control in C#


C# timer is used for time intervals. The timer class in C# represents a timer control that repeatedly runs a code block at specific intervals. Such as file backup, delete, upload, and so on in every second. The method that needs to be implemented to do this is placed inside the timer event.

Creating a Timer

We can create a Timer using a Forms designer or runtime using the TabControl class.

Forms designer

Let's create a Timer in forms designer, at first you create a windows form, then goto the toolbox = > drag the Timer and drop on the form.

creating a timer


Let's create a Timer in Runtime,then you use the Timer class.

this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label1 = new System.Windows.Forms.Label();
// label1
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(96, 68);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(36, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Time:-";
// Form1
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "Form1";
this.Text = "Form1";

Properties of Timer

We will learn about all the properties of the timer and use them.

  • Name

    Indicates the name used in code to identify the object.

    Form design of Name :
    Name of timer
  • Enabled

    Indicates the timer control are enabled or not.

    Form design of Enabled :
    Enabled of timer
    Run-time design of Enabled :
    timer1.Enabled = true;
  • GenerateMember

    Indicates if a member variable will be generated for this component.

    Form design of GenerateMember :
    GenerateMember of timer
  • Interval

    The frequency of Elapsed event in milliseconds.

    Form design of Interval :
    Interval of timer
    Run-time design of Interval :
    timer1.Interval = 2000;
  • Tick

    Occurs whenever the specified interval time elapses.

    Form design of Tick :
    Tick of timer
    Run-time design of Tick :
    timer1.Tick+=new EventHandler(timer1_Tick);
    private void timer1_Tick(object sender, EventArgs e)
        //code here
  • Start()

    Start() starts the timer control.

    Run-time design of Start() :
  • Dispose()

    Dispose() released all resource there are used by timer.

    Run-time design of Dispose() :
  • Stop()

    Stop() stops the timer control.

    Run-time design of Stop() :
  • Example of Timer

    private void Form1_Load(object sender, EventArgs e)
        timer1.Interval = 1000;
    private void timer1_Tick(object sender, EventArgs e)
        label1.Text = DateTime.Now.ToLongTimeString();


    output1 of timer output2 of timer

Write a comment

Name Email Message