Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
876 views
in Technique[技术] by (71.8m points)

c# - System.Timers.Timer timer1_Elapsed not firing! Help!

I am creating another windows service and my timer is not ticking and I have no idea why! I am using system.timers.timer as I have in previous services and it doesn't work. I have tried attaching to it but it doesn't seem to do anything.

My code:

    namespace ExpiryNotifier
{
    public partial class ExpiryNotifier : ServiceBase
    {
        public ExpiryNotifier()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("ExpiryNotifier"))
            {
                System.Diagnostics.EventLog.CreateEventSource("ExpiryNotifier", "ExpiryNotifier");
            }
            eventLog1.Source = "ExpiryNotifier";
            eventLog1.Log = "ExpiryNotifier";
        }
        private Timer timer1 = new Timer();
        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("Service Started");
            timer1.Elapsed += timer1_Elapsed;
            timer1.Interval = 10000;
            timer1.Enabled = true;

        }

        protected override void OnStop()
        {
            eventLog1.WriteEntry("Service Stopped");
            timer1.Enabled = false;

        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            eventLog1.WriteEntry("timer tick");
            timer1.Stop();

            EmailerService.EmailerService service = new EmailerService.EmailerService();
            DataSet expiringQualifications = service.GetDetailsOfExpiringQualifications();

            if(expiringQualifications.Tables[0].Rows.Count>0)
            {
                foreach(DataRow rw in expiringQualifications.Tables[0].Rows)
                {
                    if (!string.IsNullOrEmpty(rw["EmailAddress"].ToString()) )
                    {
                        if (rw["QualAwardDescription"] != null)
                        {
                            service.SendQualExpiryEmail(rw["EmailAddress"].ToString(), rw["firstName"].ToString(),
                                                        rw["QualAwardDescription"].ToString());
                        }
                    }
                }
            }


            timer1.Start();
        }
    }
}

Can anyone see the problem?

Thanks in advance!

Bex

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

System.Timers.Timer is an ugly timer. One nasty thing it does is swallow exceptions raised by the Elapsed event handler. Which will kill your timer since you stop it when entering the method. No notification whatsoever, it just stops working.

You have to at least add exception handling to this code so you can log the exception and stop the service.

Also beware the bug in your OnStart() method, you'll keep adding an event handler each time the service gets started. The Elapsed event runs multiple times, in itself a good way to bomb something.

Consider System.Threading.Timer, it doesn't have any of these problems.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...