Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/run-procedures-at-set-intervals-with-wf-timer-component.md
Pooja Poojari a34ff0fa72 Fixed duplicate titles for US-1889327 (#1270)
* Fixed duplicate titles for US-1889327

* Implemented peer review comment.

* Implemented peer review comment for US-1889327

* Implemented peer review comment for US-1889327

* Implemented peer review comment for US-1889327

* Minor fixes for US-1889327

* Metadata updates for US-1889327
2022-01-13 10:49:22 -08:00

8.9 KiB

title, description, ms.date, ms.custom, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date ms.custom dev_langs helpviewer_keywords ms.assetid
Run Procedures at Set Intervals with Timer Component Learn how use the Windows Form Timer component to run procedures at set intervals or when a set time interval has elapsed. 01/12/2022 devdivchpfy22
csharp
vb
cpp
examples [Windows Forms], timers
timers [Windows Forms], event intervals
initialization [Windows Forms], Timer components
timers [Windows Forms], Windows-based
Timer component [Windows Forms], initializing
procedures [Windows Forms], specific time intervals
8025247a-2de4-4d86-b8ab-a8cb8aeab2ea

How to: Run Procedures at Set Intervals with the Windows Forms Timer Component

You might sometimes want to create a procedure that runs at specific time intervals until a loop has finished or that runs when a set time interval has elapsed. The xref:System.Windows.Forms.Timer component makes such a procedure possible.

This component is designed for a Windows Forms environment. If you need a timer that is suitable for a server environment, see Introduction to Server-Based Timers.

Note

There are some limitations when using the xref:System.Windows.Forms.Timer component. For more information, see Limitations of the Windows Forms Timer Component's Interval Property.

To run a procedure at set intervals with the Timer component

  1. Add a xref:System.Windows.Forms.Timer to your form. See the following Example section for an illustration of how to do this programmatically. Visual Studio also has support for adding components to a form. Also see How to: Add Controls Without a User Interface to Windows Forms.

  2. Set the xref:System.Windows.Forms.Timer.Interval%2A property (in milliseconds) for the timer. This property determines how much time will pass before the procedure is run again.

    Note

    The more often a timer event occurs, the more processor time is used in responding to the event. This can slow down overall performance. Do not set a smaller interval than you need.

  3. Write appropriate code in the xref:System.Windows.Forms.Timer.Tick event handler. The code you write in this event will run at the interval specified in the xref:System.Windows.Forms.Timer.Interval%2A property.

  4. Set the xref:System.Windows.Forms.Timer.Enabled%2A property to true to start the timer. The xref:System.Windows.Forms.Timer.Tick event will begin to occur, running your procedure at the set interval.

  5. At the appropriate time, set the xref:System.Windows.Forms.Timer.Enabled%2A property to false to stop the procedure from running again. Setting the interval to 0 does not cause the timer to stop.

First code example

This first code example tracks the time of day in one-second increments. It uses a xref:System.Windows.Forms.Button, a xref:System.Windows.Forms.Label, and a xref:System.Windows.Forms.Timer component on a form. The xref:System.Windows.Forms.Timer.Interval%2A property is set to 1000 (equal to one second). In the xref:System.Windows.Forms.Timer.Tick event, the label's caption is set to the current time. When the button is clicked, the xref:System.Windows.Forms.Timer.Enabled%2A property is set to false, stopping the timer from updating the label's caption. The following code example requires that you have a form with a xref:System.Windows.Forms.Button control named Button1, a xref:System.Windows.Forms.Timer control named Timer1, and a xref:System.Windows.Forms.Label control named Label1.

Private Sub InitializeTimer()  
   ' Run this procedure in an appropriate event.  
   ' Set to 1 second.  
   Timer1.Interval = 1000  
   ' Enable timer.  
   Timer1.Enabled = True  
   Button1.Text = "Enabled"  
End Sub  
x  
Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick  
' Set the caption to the current time.  
   Label1.Text = DateTime.Now  
End Sub  
  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
      If Button1.Text = "Stop" Then  
         Button1.Text = "Start"  
         Timer1.Enabled = False  
      Else  
         Button1.Text = "Stop"  
         Timer1.Enabled = True  
      End If  
End Sub  
private void InitializeTimer()  
{  
    // Call this procedure when the application starts.  
    // Set to 1 second.  
    Timer1.Interval = 1000;  
    Timer1.Tick += new EventHandler(Timer1_Tick);  
  
    // Enable timer.  
    Timer1.Enabled = true;  
  
    Button1.Text = "Stop";  
    Button1.Click += new EventHandler(Button1_Click);  
}  
  
private void Timer1_Tick(object Sender, EventArgs e)
{  
   // Set the caption to the current time.  
   Label1.Text = DateTime.Now.ToString();  
}  
  
private void Button1_Click(object sender, EventArgs e)  
{  
  if ( Button1.Text == "Stop" )  
  {  
    Button1.Text = "Start";  
    Timer1.Enabled = false;  
  }  
  else  
  {  
    Button1.Text = "Stop";  
    Timer1.Enabled = true;  
  }  
}  
private:  
   void InitializeTimer()  
   {  
      // Run this procedure in an appropriate event.  
      // Set to 1 second.  
      timer1->Interval = 1000;  
      // Enable timer.  
      timer1->Enabled = true;  
      this->timer1->Tick += gcnew System::EventHandler(this,
                               &Form1::timer1_Tick);  
  
      button1->Text = S"Stop";  
      this->button1->Click += gcnew System::EventHandler(this,
                               &Form1::button1_Click);  
   }  
  
   void timer1_Tick(System::Object ^ sender,  
      System::EventArgs ^ e)  
   {  
      // Set the caption to the current time.  
      label1->Text = DateTime::Now.ToString();  
   }  
  
   void button1_Click(System::Object ^ sender,  
      System::EventArgs ^ e)  
   {  
      if ( button1->Text == "Stop" )  
      {  
         button1->Text = "Start";  
         timer1->Enabled = false;  
      }  
      else  
      {  
         button1->Text = "Stop";  
         timer1->Enabled = true;  
      }  
   }  

Second code example

This second code example runs a procedure every 600 milliseconds until a loop has finished. The following code example requires that you have a form with a xref:System.Windows.Forms.Button control named Button1, a xref:System.Windows.Forms.Timer control named Timer1, and a xref:System.Windows.Forms.Label control named Label1.

' This variable will be the loop counter.  
Private counter As Integer  
  
Private Sub InitializeTimer()  
   ' Run this procedure in an appropriate event.  
   counter = 0  
   Timer1.Interval = 600  
   Timer1.Enabled = True  
End Sub  
  
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick  
   If counter => 10 Then  
      ' Exit loop code.  
      Timer1.Enabled = False  
      counter = 0  
   Else  
      ' Run your procedure here.  
      ' Increment counter.  
      counter = counter + 1  
      Label1.Text = "Procedures Run: " & counter.ToString  
   End If  
End Sub  
// This variable will be the loop counter.  
private int counter;  
  
private void InitializeTimer()  
{  
   // Run this procedure in an appropriate event.  
   counter = 0;  
   timer1.Interval = 600;  
   timer1.Enabled = true;  
   // Hook up timer's tick event handler.  
   this.timer1.Tick += new System.EventHandler(this.timer1_Tick);  
}  
  
private void timer1_Tick(object sender, System.EventArgs e)
{  
   if (counter >= 10)
   {  
      // Exit loop code.  
      timer1.Enabled = false;  
      counter = 0;  
   }  
   else  
   {  
      // Run your procedure here.  
      // Increment counter.  
      counter = counter + 1;  
      label1.Text = "Procedures Run: " + counter.ToString();  
      }  
}  
private:  
   int counter;  
  
   void InitializeTimer()  
   {  
      // Run this procedure in an appropriate event.  
      counter = 0;  
      timer1->Interval = 600;  
      timer1->Enabled = true;  
      // Hook up timer's tick event handler.  
      this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);  
   }  
  
   void timer1_Tick(System::Object ^ sender,  
      System::EventArgs ^ e)  
   {  
      if (counter >= 10)
      {  
         // Exit loop code.  
         timer1->Enabled = false;  
         counter = 0;  
      }  
      else  
      {  
         // Run your procedure here.  
         // Increment counter.  
         counter = counter + 1;  
         label1->Text = String::Concat("Procedures Run: ",  
            counter.ToString());  
      }  
   }  

See also