This documentation is archived and is not being maintained.

Timer.Enabled Property

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

[Visual Basic]
Public Property Enabled As Boolean
[C#]
public bool Enabled {get; set;}
[C++]
public: __property bool get_Enabled();
public: __property void set_Enabled(bool);
[JScript]
public function get Enabled() : Boolean;
public function set Enabled(Boolean);

Property Value

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

Remarks

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

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.

Example

[Visual Basic, C#, C++] The following example creates a Timer that displays "Hello World!" on the console every five seconds.

[Visual Basic, C#, C++] Use the System.Timers namespace for this example.

[Visual Basic] 
Public Class Timer1
    
    Public Shared Sub Main()
        Dim aTimer As New System.Timers.Timer()
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        ' Set the Interval to 5 seconds.
        aTimer.Interval = 5000
        aTimer.Enabled = True
        
        Console.WriteLine("Press 'q' to quit the sample.")
        While Console.Read() <> CInt("q")
        End While
    End Sub
        
    ' Specify what you want to happen when the Elapsed event is raised.
    Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine("Hello World!")
    End Sub
End Class


[C#] 
public class Timer1
 {
 
     public static void Main()
     {
         System.Timers.Timer aTimer = new System.Timers.Timer();
         aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
         // Set the Interval to 5 seconds.
         aTimer.Interval=5000;
         aTimer.Enabled=true;
 
         Console.WriteLine("Press \'q\' to quit the sample.");
         while(Console.Read()!='q');
     }
 
     // Specify what you want to happen when the Elapsed event is raised.
     private static void OnTimedEvent(object source, ElapsedEventArgs e)
     {
         Console.WriteLine("Hello World!");
     }
 }
 

[C++] 
public __gc class Timer1
{
public:
    static void Main() {
        System::Timers::Timer* aTimer = new System::Timers::Timer;
        aTimer->Elapsed += new ElapsedEventHandler(0, Timer1::OnTimedEvent);
        // Set the Interval to 5 seconds.
        aTimer->Interval=5000;
        aTimer->Enabled=true;
    }
private:
    // Specify what you want to happen when the Elapsed event is raised.
     static void OnTimedEvent(Object* /*source*/, ElapsedEventArgs* /*e*/)
     {
         Console::WriteLine(S"Hello World!");
     }
};

int main()
{
    Timer1::Main();

    Console::WriteLine(S"Press \'q\' to quit the sample.");
    while(Console::Read()!='q');
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Timer Class | Timer Members | System.Timers Namespace | Interval | AutoReset | Start | Stop | Elapsed

Show: