8

I have a windows service running, inside this i want to run a function every then minutes. I have found some code but it doesn't seem to work? I have a logger and it does not seem to go into the timer_Elapsed function ever?

 protected override void OnStart(string[] args)
    {
       // SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
       // test.Import();
         log.Info("Info - Service Started");
        _timer = new Timer(10 * 60 * 1000); // every 10 minutes??
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    }

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        log.Info("Info - Check time");
        DateTime startAt = DateTime.Today.AddHours(9).AddMinutes(48);
        if (_lastRun < startAt && DateTime.Now >= startAt)
        {
            // stop the timer 
            _timer.Stop();               

            try
            {
                log.Info("Info - Import");
                SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
                test.Import();
            }
            catch (Exception ex) {
                log.Error("This is my error - ", ex);
            }

            _lastRun = DateTime.Now;
            _timer.Start();
        }
    }
Beginner
  • 28,539
  • 63
  • 155
  • 235

5 Answers5

20

You need to start the timer:

protected override void OnStart(string[] args)
{
     log.Info("Info - Service Started");
    _timer = new Timer(10 * 60 * 1000); // every 10 minutes
    _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    _timer.Start(); // <- important
}
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 6
    For the 10 minutes, I prefer to use `TimeSpan.FromMinutes(10).TotalMilliseconds;` instead of `10 * 60 * 1000` – Jaider Aug 15 '16 at 16:23
4

I don't see _timer.Start(), that should be your problem.

Tigran
  • 61,654
  • 8
  • 86
  • 123
3

Daniel Hilgarth is correct - the main issue is that you never call Start on the timer.

That being said, you might want to also consider using the Windows Task Scheduler instead of a service with a timer. This allows you to schedule the task to run every 10 minutes, but also change the schedule whenever desired without a compilation change.

Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
3

I need this functionality also. That is, my C# windows service must check email every 10 minutes. I stripped down some logic to make the code more effective, as follows :

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            _timer.Stop();
            try
            {
                EventLog.WriteEntry(Program.EventLogName, "Checking emails " + _count++);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(Program.EventLogName, "This is my error " + ex.Message);
            }
            _timer.Start();
        }

The timer_elapsed method indeed will be call every 10 minutes, starting from the first _timer.start(), which you miss it by the way. I haven't done any checking of the _lastRun and startAt. I don't think we need it

swdev
  • 4,997
  • 8
  • 64
  • 106
1

Try starting the timer,

protected override void OnStart(string[] args)
    {
       // SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
       // test.Import();
         log.Info("Info - Service Started");
        _timer = new Timer(10 * 60 * 1000); // every 10 minutes??
            _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start();
    }

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        log.Info("Info - Check time");
        DateTime startAt = DateTime.Today.AddHours(9).AddMinutes(48);
        if (_lastRun < startAt && DateTime.Now >= startAt)
        {
            // stop the timer 
            _timer.Stop();               

            try
            {
                log.Info("Info - Import");
                SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
                test.Import();
            }
            catch (Exception ex) {
                log.Error("This is my error - ", ex);
            }

            _lastRun = DateTime.Now;
            _timer.Start();
        }
    }
FIre Panda
  • 6,537
  • 2
  • 25
  • 38