Solved

windows service doesn't run based on Timer ?

Posted on 2012-04-04
7
367 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 74

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 74

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 74

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

22 Experts available now in Live!

Get 1:1 Help Now