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
Please, read about Constructor
your initialization code should not be in public
static void Main(), but instead inpublic TimerClass()also your methods and aTimer should not be static.