Question: I create a windows service to run a piece of code and implemented timer in it to run it periodically.

My timer class is :

```
class TimerClass
{
    private static System.Timers.Timer aTimer;

    public static void Main()
    {

        aTimer = new System.Timers.Timer(1000);

        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        aTimer.Interval = 5000;
        aTimer.Enabled = true;

        GC.KeepAlive(aTimer);
    }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        aTimer.Stop();
        DatabaseUpdation dbUp = new DatabaseUpdation();
        File.AppendAllText(@"C:\Documents and Settings\New Folder\My Documents\demo\abc.txt", "Start" + " " + DateTime.Now.ToString() + Environment.NewLine);
        dbUp.GetDatafromSource();

        aTimer.Start();
    }

}

```

And i am calling it from my Start method:

```
    protected override void OnStart(string[] args)
    {
        TimerClass timer = new TimerClass();
    }

```

But timer is not executing at all.
Can anyone find me the mistake here?
Thanks in advance

Answer: 
Please, read about [Constructor](http://msdn.microsoft.com/en-us/library/vstudio/ace5hbzh.aspx)

your initialization code should not be in public `static void Main()`, but instead in `public TimerClass()`

```
class TimerClass
{
    private System.Timers.Timer aTimer;

    public TimerClass()
    {
        aTimer = new System.Timers.Timer(1000);
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Interval = 5000;
        aTimer.Enabled = true;
        GC.KeepAlive(aTimer);
    }

    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        aTimer.Stop();
        DatabaseUpdation dbUp = new DatabaseUpdation();
        File.AppendAllText(@"C:\Documents and Settings\New Folder\My Documents\demo\abc.txt", "Start" + " " + DateTime.Now.ToString() + Environment.NewLine);
        dbUp.GetDatafromSource();
        aTimer.Start();
    }
}

```

also your methods and aTimer should not be static.
You need to call `Main` method to start timer:

```
protected override void OnStart(string[] args)
{
    TimerClass.Main();
}

```

BTW not very good name - I think something like `Start` will be better. Also I hope this is not your app entry point method.