Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Windows Service doesn't start based on the interval time

Posted on 2012-04-03
5
Medium Priority
?
362 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 45

Accepted Solution

by:
AndyAinscow earned 1400 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 600 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 45

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

886 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