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: 
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.
Try this: 

```
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;

            GC.KeepAlive(aTimer);

        }
        public static void Start()
        {
           aTimer.Start();

        }

        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 you need to call it like this:

```
protected override void OnStart(string[] args)
    {
        //TimerClass timer = new TimerClass();<==No need it's static class!!!
        TimerClass.Main();
        TimerClass.Start();
    }

```