0

I have created a timer in window service ,which run after on system startup ..I want to start it at particular time , lets say : 3:00 PM ..

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

On similar Posts .. it is not mentioned How to set it on particular day .. Any suggestion would be helpful

Neeraj Verma
  • 2,174
  • 6
  • 30
  • 51
  • Which Timer class are you using? System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer – Mike Hixson Jun 13 '14 at 04:31
  • I am using System.Timers.Timer – Neeraj Verma Jun 13 '14 at 04:42
  • How about setting the interval for 1 minute. On your elapsed event you can check to see if the current time is 3:00. If its not 3:00 then just return. – Mike Hixson Jun 13 '14 at 04:55
  • You mean you want to start the timer at particular time? – Ricky Jun 13 '14 at 05:10
  • yes .. I mean you want to start the timer at particular time – Neeraj Verma Jun 13 '14 at 07:12
  • Maybe you can start from here: http://stackoverflow.com/questions/3243348/how-to-call-a-method-daily-at-specific-time-in-c http://stackoverflow.com/questions/15417851/run-system-timers-timer-event-on-specific-day-and-time-each-week-c-sharp http://stackoverflow.com/questions/20282144/code-for-executing-method-every-day-at-specific-time-c-sharp-windows-service-f – Jack Malkovich Jun 13 '14 at 08:50
  • Looks like you are writing a job scheduling package - try using the off-the-shelf Quartz library ? http://www.quartz-scheduler.net/ – PhillipH Jun 13 '14 at 10:57

1 Answers1

0

Have you tried with - FluentSheduler ?

You can initialize it at Service Start

brainless coder
  • 6,310
  • 1
  • 20
  • 36
  • thanks for the tool . I Tried to add it as per you say .. But still it is not executing anything except start and stop log – Neeraj Verma Jun 14 '14 at 07:55