窗口服务在特定时间设置计时器

时间:2014-06-13 04:24:38

标签: c# .net service timer

我在窗口服务中创建了一个计时器,它在系统启动后运行。我想在特定时间启动它,让我们说:3:00 PM ..

这是我试过的

private Timer scheduleTimer = null;
        private DateTime lastRun;
        private bool flag;

        public AutoSMSService()
        {
            InitializeComponent();

            if (!System.Diagnostics.EventLog.SourceExists("AutoSMSSource"))
            { System.Diagnostics.EventLog.CreateEventSource("AutoSMSSource", "AutoSMSLog"); }
            eventLogAutoSMS.Source = "AutoSMSSource";
            eventLogAutoSMS.Log = "AutoSMSLog";
            scheduleTimer = new Timer();
            scheduleTimer.Interval = 5000;


        }


        protected override void OnStart(string[] args)
        {
            flag = true;
            lastRun = DateTime.Now;
            scheduleTimer.Start();

            //some operation
        }

        protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (flag == true)
            {


                lastRun = DateTime.Now;
                flag = false;
            }
            else if (flag == false)
            {


                if (lastRun.Date < DateTime.Now.Date)
                {
                    eventLogAutoSMS.WriteEntry("DB Call after Interval");
                    ASMSFetch.Program.UpdateSMS();
                }
            }
        }

        protected override void OnStop()
        {
            eventLogAutoSMS.WriteEntry("Stopped");
        }

在类似的帖子上..没有提到如何在特定的日子设置它.. 任何建议都会有所帮助

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用 - FluentSheduler

您可以在Service Start

初始化它