C# Timer Timer()

Description

Timer Timer() Initializes a new instance of the Timer class, and sets all the properties to their initial values.

Syntax

Timer.Timer() has the following syntax.


public Timer()

Example


using System;/*  w w  w. j  av  a  2 s  .c  o 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