C# Timer Enabled

Description

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

Syntax

Timer.Enabled has the following syntax.


[TimersDescriptionAttribute("TimerEnabled")]
public bool Enabled { get; set; }

Example


using System;//ww w . ja va2s  .co  m
using System.Timers;

public class Timer1
{
    private static System.Timers.Timer aTimer;

    public static void Main()
    {
        aTimer = new System.Timers.Timer(10000);
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Interval = 2000;
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();

        //GC.KeepAlive(aTimer);
    }
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Timers »




Timer