System.Timers.Timer timer1_Elapsed 未触发!帮助! [英] System.Timers.Timer timer1_Elapsed not firing! Help!

查看:29
本文介绍了System.Timers.Timer timer1_Elapsed 未触发!帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建另一个 Windows 服务,但我的计时器没有计时,我不知道为什么!我正在使用 system.timers.timer ,因为我在以前的服务中使用过,但它不起作用.我试过附加到它,但它似乎没有做任何事情.

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.

我的代码:

    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();
        }
    }
}

有人能看到问题吗?

提前致谢!

贝克

推荐答案

System.Timers.Timer 是一个丑陋的计时器.它所做的一件令人讨厌的事情是吞下由 Elapsed 事件处理程序引发的异常.这将杀死您的计时器,因为您在进入该方法时将其停止.没有任何通知,它只是停止工作.

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.

还要注意 OnStart() 方法中的错误,每次服务启动时,您将不断添加事件处理程序.Elapsed 事件运行多次,这本身就是一种轰炸某些东西的好方法.

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.

考虑 System.Threading.Timer,它没有任何这些问题.

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

这篇关于System.Timers.Timer timer1_Elapsed 未触发!帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆