2

I wrote a windows service to call my class library every 10 mins interval,it works fine when start or restart .once the job done it suppose to re run again every 10 min's that's not happening at all.Am not sure what am missing,some one please identify the correct way.

Here is my code

 public partial class Service1 : ServiceBase
{
    private Timer _timer;
    private DateTime _lastRun = DateTime.Now;

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();

        _timer = new Timer(10 * 60 * 1000); // every 10 minutes
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        Shell Distribute= new Shell();
        Distribute.Distribute();
    }

    protected override void OnStop()
    {
        this.ExitCode = 0;
        base.OnStop();

    }
    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
       //if (_lastRun.Date < DateTime.Now.Date)
        //{
            _timer.Stop();
           _lastRun = DateTime.Now;
            _timer.Start();
         //}
        }

    }
}
Usher
  • 2,078
  • 8
  • 39
  • 74
  • 1
    The Date comparison is the most likely cause. However, the Timer class swallows exceptions when it fires the Elapsed event. You *must* use try/catch to diagnose any failures in your code. Don't skip it, exceptions are quite undiagnosable if you do. – Hans Passant Apr 03 '12 at 13:52
  • @HansPassant,even i commented If Conditional statement still its not running the service every 10 mins interval – Usher Apr 05 '12 at 01:36

3 Answers3

3

Your problem is compare of date if (_lastRun.Date < DateTime.Now.Date) so your code runs once a day.

Özgür Kara
  • 1,315
  • 10
  • 20
  • i completely changed my code and commented those part still its not running based on time interval,am not sure how to debug this one. – Usher Apr 05 '12 at 01:25
2

I agree with Ozgur. It appears that your logic is wrong. You can just stop the timer during the timer_Elapsed event do you logic and restart timers

Something like :

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{

    try{
        // stop the timer while we are running the cleanup task

        _timer.Stop();
        //
        // do cleanup stuff
        //
    }catch (Exception e){
         //do your error handling here.
    }
    finally{

       _timer.Start();
    }

    }
}

Just wrap it with a try catch and finally so you handle exceptions and can make sure the timer is started again. Also please review this link Best Timer for using in a Windows service

Community
  • 1
  • 1
Namphibian
  • 11,288
  • 6
  • 43
  • 68
  • Can I make a suggestion here. Why not put some form of logging into the service. Then write debug messages into the log. I normally use a simple text file with debug information like. This can help you with tracing the problems. Also please make sure you log exceptions. – Namphibian Apr 05 '12 at 06:48
0

Okie Finally i got the answer,why its not working (One of the expert from other forum point out my mistake)

This the code works well based on timer interval.

     public partial class Service1 : ServiceBase
{
    private Timer _timer;
    private DateTime _lastRun = DateTime.Now;

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();

        _timer = new Timer(10 * 60 * 1000); // every 10 minutes
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        Shell Distribute= new Shell();
        Distribute.Distribute();
_timer.start();//this line was missed in my original code
    }

    protected override void OnStop()
    {
        this.ExitCode = 0;
        base.OnStop();

    }
    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
       //if (_lastRun.Date < DateTime.Now.Date)
        //{

try
{
_timer.Stop();
Shell Distribute= new Shell();
Distribute.Distribute();
}
catch(exception ex)
{}
finally
{

            _timer.Start();
}
         //}
        }

    }
}
Usher
  • 2,078
  • 8
  • 39
  • 74
  • Makes sense. I did not read your question properly now that I look at it. You do start the job on service start event but never enable the timer. I think I need new spectacles. You did say works fine when start or restart which is a major hint. I would add some logging to the catch block so you can see if anything goes wrong during the timer event. Nothing worse than a service that swallows errors. – Namphibian Apr 05 '12 at 12:39
  • I agree Namphibian,i will add the log to find what's going on when the service start. – Usher Apr 06 '12 at 01:09