C# Timer Interval

Description

Timer Interval Gets or sets the interval at which to raise the Elapsed event.

Syntax

Timer.Interval has the following syntax.


[TimersDescriptionAttribute("TimerInterval")]
[SettingsBindableAttribute(true)]
public double Interval { get; set; }

Example


using System;/*from   w  ww . j  ava 2s  .  c  om*/
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