Timer Class
Collapse the table of content
Expand the table of content

Timer Class


Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in Windows Forms applications and must be used in a window.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)


public class Timer : Component


Initializes a new instance of the Timer class.


Initializes a new instance of the Timer class together with the specified container.


Gets a value indicating whether the component can raise an event.(Inherited from Component.)


Gets the IContainer that contains the Component.(Inherited from Component.)


Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)


Gets or sets whether the timer is running.


Gets the list of event handlers that are attached to this Component.(Inherited from Component.)


Gets or sets the time, in milliseconds, before the Tick event is raised relative to the last occurrence of the Tick event.


Gets or sets the ISite of the Component.(Inherited from Component.)


Gets or sets an arbitrary string representing some type of user state.


Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)


Releases all resources used by the Component.(Inherited from Component.)


Disposes of the resources, other than memory, used by the timer.(Overrides Component.Dispose(Boolean).)


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.(Inherited from Component.)


Serves as the default hash function. (Inherited from Object.)


Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)


Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)


Gets the Type of the current instance.(Inherited from Object.)


Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)


Raises the Tick event.


Starts the timer.


Stops the timer.


This API supports the product infrastructure and is not intended to be used directly from your code. Returns a string that represents the Timer.(Overrides Component.ToString().)


Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)


Occurs when the specified timer interval has elapsed and the timer is enabled.

A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.

When you use this timer, use the Tick event to perform a polling operation or to display a splash screen for a specified period of time. Whenever the Enabled property is set to true and the Interval property is greater than zero, the Tick event is raised at intervals based on the Interval property setting.

This class provides methods to set the interval, and to start and stop the timer.


The Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds. If you require a multithreaded timer with greater accuracy, use the Timer class in the System.Timers namespace.

The following example implements a simple interval timer, which sets off an alarm every five seconds. When the alarm occurs, a MessageBox displays a count of the number of times the alarm has started and prompts the user as to whether the timer should continue to run.

public class Class1 {
    static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    static int alarmCounter = 1;
    static bool exitFlag = false;

    // This is the method to run when the timer is raised.
    private static void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs) {

       // Displays a message box asking whether to continue running the timer.
       if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter, 
          MessageBoxButtons.YesNo) == DialogResult.Yes) {
          // Restarts the timer and increments the counter.
          alarmCounter +=1;
          myTimer.Enabled = true;
       else {
          // Stops the timer.
          exitFlag = true;

    public static int Main() {
       /* Adds the event and the event handler for the method that will 
          process the timer event to the timer. */
       myTimer.Tick += new EventHandler(TimerEventProcessor);

       // Sets the timer interval to 5 seconds.
       myTimer.Interval = 5000;

       // Runs the timer, and raises the event.
       while(exitFlag == false) {
          // Processes all the events in the queue.
    return 0;

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft