Solved

Windows Service doesn't start based on the interval time

Posted on 2012-04-03
5
350 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What namespace do I need to import? 2 38
Hey Audio! 9 44
Getting error in connectionstring with Excel. 30 46
Problem with SelectList in .NET MVC application 1 28
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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