Solved

windows service doesn't run based on Timer ?

Posted on 2012-04-04
7
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 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