Solved

Windows Service doesn't start based on the interval time

Posted on 2012-04-03
5
349 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
ID: 37800729
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
ID: 37805361
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
ID: 37805443
Thanks both of you guys to point out my mistake,thanks again.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37805576
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
ID: 37805617
Which is why I stated: "please note I am *not* recommending that at all"

:-)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

815 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

12 Experts available now in Live!

Get 1:1 Help Now