1

can any one please check what i am doing onstart is correct or not please. i am getting error service started and stopped automatically. i want to run my service every 10 minutes. please help its been 4 hours i am struggling

 protected override void OnStart(string[] args)
        {
            try
            {
                _aTimer.Start();

                _aTimer.Enabled = true;
                _aTimer = new System.Timers.Timer(10 * 60 * 1000);//10 minutes 
                _aTimer.AutoReset = true;
                _aTimer.Elapsed += new System.Timers.ElapsedEventHandler(_aTimer_Elapsed);
            }
            catch(Exception ex) {
                RMLogger.RMException("ServiceManager", this.GetType().Name.ToString(), ex.ToString());
            }




        }
Tan
  • 778
  • 3
  • 18
  • 36

3 Answers3

2

Make sure you have the same .net framework version you are compiling to to the server you are installing the windows service

I had the same issue, I was compiling it in 4.5.2 and the server 4.5. The installutil tricks us allowing it to be installed but it doesn't start.

RollRoll
  • 8,133
  • 20
  • 76
  • 135
0

For what I see what goes wrong here is the following:

  1. You start your timer.
  2. You enable it.
  3. Then you give the timer a new instance.

I think that the timer automatically resets at the new given instance.

Try give it a new instance first and the required parameters to run the timer. Then start the timer.

fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
  • i am confused over calling timer_elapsed event, i want my service to run every 10 minutes i can't use windows task scheduler..can i set the timer elapsed after the InitializeComponent() ? what you said is right because of new instance given to timer which is 10 minutes and the max time for the windows service to start is only 30 seconds, is causing the problem – Tan Apr 19 '16 at 08:48
  • I suggest you do, because if ( it may occur ) that the timer elapses before the initializecomponents and the elapsed events is calling a component, you would definitly get an error about a component which is still null. – Chris van der Luijt Apr 19 '16 at 08:54
  • i just moved my code _aTimer = new System.Timers.Timer(10 * 60 * 1000);//10 minutes under initializingcomponent() and installed the service it started successfully. but need to check if it will run as desired or not. thanks for your inputs mate. it was helpful – Tan Apr 19 '16 at 08:56
  • Your welcome, glad to help! I hope you have got the desired situation. Good luck with your service. – Chris van der Luijt Apr 19 '16 at 14:13
0

I think you have things a little confused here. You say

i want to run my service every 10 minutes.

but the timer that you are starting will run every 10 minutes only while the service is continually running.

Either you mean what you way and want to start the service every 10 minutes, in which case I would use something like Task Scheduler to start the service every 10 minutes, let it do it's processing, and then shut down again.

Or you just let your service run continually and the timer event will fire every 10 minutes.

Barry O'Kane
  • 1,189
  • 7
  • 12
  • i am not supposed to use task scheduler that is why i am setting my timer to 10 minutes and because of that the service is starting and stopping as on start i am setting my timer interval to 10 minutes may be. – Tan Apr 19 '16 at 08:41
  • can i specify the timer leapsed even after initializecomponent()? – Tan Apr 19 '16 at 08:42
  • Have a look at this post http://stackoverflow.com/questions/12885013/windows-service-with-timer – Barry O'Kane Apr 19 '16 at 09:01
  • my service should create a excel file, its running created folders etc but unable to create excel file do i need to give any permission for the service? – Tan Apr 19 '16 at 09:37
  • I think that's a separate question, and you'd need to post the code. As a first step, put in some exception handling and see what it's failing on. That'll give you a good starting point. – Barry O'Kane Apr 19 '16 at 10:07