Solved

windows service doesn't run based on Timer ?

Posted on 2012-04-04
7
372 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
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.

 
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 500 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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