-->![Form Form](http://help.solidworks.com/2015/English/api/epdmapi/SendMessage.gif)
Creating a simple Stopwatch/Timer application with C# and Windows Forms. While working on a project today, I came across the need for a simple stopwatch/timer which would keep track of the total elapsed time until it was reset. Creating a simple Stopwatch/Timer application with C# and Windows Forms. While working on a project today, I came across the need for a simple stopwatch/timer which would keep track of the total elapsed time until it was reset.
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 Timer component makes such a procedure possible.
![Form Form](http://help.solidworks.com/2015/English/api/epdmapi/SendMessage.gif)
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 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
- Add a 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.
- Set the Interval property (in milliseconds) for the timer. This property determines how much time will pass before the procedure is run again.NoteThe 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.
- Write appropriate code in the Tick event handler. The code you write in this event will run at the interval specified in the Interval property.
- Set the Enabled property to
true
to start the timer. The Tick event will begin to occur, running your procedure at the set interval. - At the appropriate time, set the Enabled property to
false
to stop the procedure from running again. Setting the interval to0
does not cause the timer to stop.
Example
This first code example tracks the time of day in one-second increments. It uses a Button, a Label, and a Timer component on a form. The Interval property is set to 1000 (equal to one second). In the Tick event, the label's caption is set to the current time. When the button is clicked, the Enabled 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 Button control named Button1
, a Timer control named Timer1
, and a Label control named Label1
.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 Button control named
Button1
, a Timer control named Timer1
, and a Label control named Label1
.