Solved

windows service doesn't run based on Timer ?

Posted on 2012-04-04
7
375 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
Technology Partners: 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Save json data from URL using SSIS 1 77
Selenium and Xpath 4 69
How does this modal gets closed? 6 42
Which is best Image Resizing Web service 11 42
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…
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!
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

751 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