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

x
?
Solved

windows service doesn't run based on Timer ?

Posted on 2012-04-04
7
Medium Priority
?
380 Views
Last Modified: 2012-04-07
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)
    {
       //if (_lastRun.Date < DateTime.Now.Date)
        //{
            _timer.Stop();
           _lastRun = DateTime.Now;
            _timer.Start();
         //}
        }

    }
}

Open in new window

0
Comment
Question by:Sha1395
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37809250
If by "it suppose to re run again every 10 min's" you mean that the code in your Shell class isn't run again, then that is because you are only executing it within the OnStart method of the service. You would need to create another instance within the timer_Elapsed handler and execute the Distribute method again. In your current setup, the instance you created only exists during the OnStart method--once that terminates, no more instance.
0
 

Author Comment

by:Sha1395
ID: 37809256
Thanks Kaufmed, even i tried to create another instance in timer_elapsed handler like below,but it doesn't work either.

 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 fileDistributor = new Shell();
            fileDistributor.Distribute();
           
        }

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

        }
        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
         
            try
            {
                _timer.Stop();
                 Shell fileDistributor = new Shell();
                  fileDistributor.Distribute();
            }
            catch (Exception ex)
            { }
            finally
            {
              _timer.Start();
            }
            //}

        

        }

Open in new window


Can you pls correct me what am doing wrong here
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 37809291
I think you might be experiencing a problem I had some time ago. The Timer just doesn't fire. It is because you're using the wrong class.

The "Timer" class by default references the System.Windows.Forms.Timer class, which is designed for form applications and does NOT work properly in a service.

You actually want to use the System.Timers.Timer class, which works in a Windows service. Specify the complete class name including namespace in your code to make sure you aren't mixing the two classes up.

See:
http://www.codeguru.com/csharp/.net/cpp_managed/windowsservices/article.php/c6919/Using-Timers-in-a-Windows-Service.htm
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37809294
@Frosty555

The Timer class found under the System.Windows.Forms namespace has a Tick event, not an Elapsed event. Based on the arguments to the handler, I think the author is using the System.Timers Timer.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37809316
You never start your timer!! Add the following  at the end of OnStart:

_timer.Start();

Open in new window


that should "get you going"  = )
0
 

Author Comment

by:Sha1395
ID: 37809424
Thanks again Kaufmed, No wonder why you stay always No 1.

Thanksssssssss a tooooon
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 37820438
Darn I'm getting rusty. Didn't see that at all. Good eye Kaufmed.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses

926 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