0

I am using Timer in Windows Service on OnStart() method handling timer_Elapsed() event check after every 5 min.

But timer_Elapsed() not firing/calling after 5 min, Please correct me If I am doing anything wrong in below code.

        protected override void OnStart(string[] args)
        {
            try
            {
                genLogs.WriteErrorLog("Service Started OnStart.");
                //int tmStart = Convert.ToInt32(ConfigurationManager.AppSettings["tim"]);
                using (Timer timer = new Timer(30000))  //  (1000 * 5 * 60) for 5 minutes
                {
                    timer.Elapsed += timer_Elapsed;
                    timer.Start();
                    //Console.WriteLine("Timer is started");
                    genLogs.WriteErrorLog("Timer is started.");
                    //Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                genLogs.WriteErrorLog("OnStart Service Error: " + ex.Message);
            }

        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                genLogs.WriteErrorLog("Service Started Checking Windows Services and Web AppPools.");

                serviceLogic.InsertStatus();

                genLogs.WriteErrorLog("Service Calls Send SendEmails Method.");
                serviceLogic.SendInfo();
            }
            catch (Exception ex)
            {
                genLogs.WriteErrorLog("OnStart Service Error: " + ex.Message);
            }
        }
Rahul Hendawe
  • 902
  • 1
  • 14
  • 39
  • use System.Threading.Timer than System.Timers.Timer , System.Threading.Timer class makes callbacks on a ThreadPool thread and does not use the event model refer to this http://stackoverflow.com/questions/1435876/do-c-sharp-timers-elapse-on-a-separate-thread and https://msdn.microsoft.com/en-us/library/zdzx8wx8.aspx – Amit Pore Jun 09 '16 at 11:28

2 Answers2

3

I think it might be because you are doing it in a using block.

using (Timer timer = new Timer(30000))  //  (1000 * 5 * 60) for 5 minutes
            {
                timer.Elapsed += timer_Elapsed;
                timer.Start();
                //Console.WriteLine("Timer is started");
                genLogs.WriteErrorLog("Timer is started.");
                //Console.ReadLine();
            } <- Your timer stops existing here

Doing it in a using block would be the same as saying timer.Dispose(); which disposes the timer and therefore it can't call any methods. This should work:

Timer timer = new Timer(30000)
timer.Elapsed += timer_Elapsed;
timer.Start();
  • but same is working on console application. Is that differs in window services? – Rahul Hendawe Jun 09 '16 at 11:12
  • I tried doing what you did in a console application but since the timer is being disposed when the using block ends it doesn't work. – Andreas Fredriksson Jun 09 '16 at 11:18
  • @RahulHendawe why you doing it in using ? if you use Threading.Timer you ca dispose that timer in delegate method – Amit Pore Jun 09 '16 at 11:33
  • You are doing it in a using, so I just tried it out to see if I was misunderstanding something. Like both me and adb has already said; The using block is disposing the timer. Doing it in a using block is the same as calling timer.Dispose(); since a using block automatically disposes the object for you. – Andreas Fredriksson Jun 09 '16 at 11:45
  • @AndreasFredriksson: Thanks..! for correcting me. Resolved. – Rahul Hendawe Jun 09 '16 at 11:59
2

disposing your timer makes it impossible to run the callback.

in case you wish to dispose it, you must synchronize that call.

Check this post -> How do I gracefully stop a System.Threading.Timer?

Community
  • 1
  • 1
Andrey Bobrov
  • 201
  • 1
  • 13