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

本文关键字:时间 服务 设置 定时器 窗口 | 更新日期: 2023-09-27 18:08:30

我在窗口服务中创建了一个计时器,它在系统启动后运行。我想在特定的时间开始,比如:下午3点。

here is what i Tried

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

在类似的帖子…没有提到如何把它定在特定的日子…任何建议都会有帮助的

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

您尝试过- FluentSheduler吗?

你可以在Service Start

初始化它