Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Enabled Property
Collapse the table of content
Expand the table of content

Timer.Enabled Property

Gets or sets a value indicating whether the Timer should raise the Elapsed event.

Namespace: System.Timers
Assembly: System (in system.dll)

public bool Enabled { get; set; }
/** @property */
public boolean get_Enabled ()

/** @property */
public void set_Enabled (boolean value)

public function get Enabled () : boolean

public function set Enabled (value : boolean)

Not applicable.

Property Value

true if the Timer should raise the Elapsed event; otherwise, false. The default is false.

Exception typeCondition

ObjectDisposedException

This property cannot be set because the timer has been disposed.

Setting Enabled to true is the same as calling Start, while setting Enabled to false is the same as calling Stop.

NoteNote:

The Elapsed event is raised on a ThreadPool thread, so the event-handling method might run on one thread at the same time that the Enabled property is set to false on another thread. This might result in the Elapsed event being raised after the Enabled property is set to false. The code example for the Stop method shows one way to prevent this race condition.

If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.

If the interval is set after the Timer has started, the count is reset. For example, if you set the interval to 5 seconds and then set the Enabled property to true, the count starts at the time Enabled is set. If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.

NoteNote:

Some visual designers, such as those in Microsoft Visual Studio, set the Enabled property to true when inserting a new Timer.

The following code example sets up an event handler for the Timer.Elapsed event, creates a timer, and uses the Enabled event to start the timer. The event handler displays the SignalTime property each time it is raised.

using System;
using System.Timers;

public class Timer1
{
    public static void Main()
    {
        // Normally, the timer is declared at the class level, so
        // that it doesn't go out of scope when the method ends.
        // In this example, the timer is needed only while Main 
        // is executing. However, KeepAlive must be used at the
        // end of Main, to prevent the JIT compiler from allowing 
        // aggressive garbage collection to occur before Main 
        // ends.
        System.Timers.Timer aTimer = new System.Timers.Timer();

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        // Set the Interval to 2 seconds (2000 milliseconds).
        aTimer.Interval = 2000;
        aTimer.Enabled = true;
 
        Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();

        // Keep the timer alive until the end of Main.
        GC.KeepAlive(aTimer);
    }
 
    // Specify what you want to happen when the Elapsed event is 
    // raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Hello World!");
    }
}
 

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft