System.Timers.Timer timer1_Elapsed没有发射!帮帮我! [英] System.Timers.Timer timer1_Elapsed not firing! Help!

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

问题描述

我创建另一个窗口服务和我的计时器滴答不,我不知道为什么!
我使用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();
        }
    }
}



任何人都可以看到这个问题?

Can anyone see the problem?

在此先感谢!

贝克斯

推荐答案

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()方法中的bug,你会保持每个服务被启动时添加事件处理程序。经过事件运行多次,本身就弹什么的好方法。

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天全站免登陆