Solved

Windows Service doesn't start based on the interval time

Posted on 2012-04-03
5
346 Views
Last Modified: 2012-04-04
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.


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)
    {
        // ignore the time, just compare the date
        if (_lastRun.Date < DateTime.Now.Date)
        {
            // stop the timer while we are running the cleanup task
            _timer.Stop();
            //
            // do cleanup stuff
            //
            _lastRun = DateTime.Now;
            _timer.Start();
        }

    }
}

Open in new window

0
Comment
Question by:Sha1395
  • 2
  • 2
5 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 350 total points
Comment Utility
From your code this line is very suspicious
// ignore the time, just compare the date

It doesn't run the timer code because you explicitly said not to, unless it is the next day.  (To make it run every ten minutes - remove the if statement)
0
 
LVL 15

Assisted Solution

by:angus_young_acdc
angus_young_acdc earned 150 total points
Comment Utility
Agreed with AndyAinscow.  With the code as you have it now you would be better off not having a Timer and just a While(true) loop then compare the last run and current datetime - please note I am *not* recommending that at all, just saying that is how the code reads how it should work.

If you really need to keep that code you should have an else statement, so it would read:
// ignore the time, just compare the date
        if (_lastRun.Date < DateTime.Now.Date)
        {
            // stop the timer while we are running the cleanup task
            _timer.Stop();
            //
            // do cleanup stuff
            //
            _lastRun = DateTime.Now;
            _timer.Start();
        }
        else
       {
             _timer.Start();
       }

Open in new window

0
 

Author Comment

by:Sha1395
Comment Utility
Thanks both of you guys to point out my mistake,thanks again.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
One smallpoint concerning the other sugestion
>>just a While(true) loop
I would not recommend that as it stands, it really needs extra stuff as well - the loop would be running at full blast continuously (100% cpu usage).
0
 
LVL 15

Expert Comment

by:angus_young_acdc
Comment Utility
Which is why I stated: "please note I am *not* recommending that at all"

:-)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now