Link to home
Start Free TrialLog in
Avatar of gautam_reddyc
gautam_reddyc

asked on

Windows Service Starts and Stops Automatically

Hi,
  I have a windows service that executes the code (a function) every 5 minutes..
When i start the service.. it starts and stops automatically..

I get the below message..
WIndow Service started and then stopped. Some services stop automatially if they no longer used by other process..

My code is below.. Pls help

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using USAir.StationCalenderService;
using System.Timers;
using System.Diagnostics;

namespace USAir.StationCalenderService
{
    public partial class StationCalenderService : ServiceBase
    {
        Timer timer = new Timer();

        public StationCalenderService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Debugger.Launch();
            getTaskIDs();

            //ad 1: handle Elapsed event
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

        //ad 2: set interval to 1 minute (= 300,000 milliseconds)

        timer.Interval = 300000;

        //ad 3: enabling the timer
        timer.Enabled = true;

           
        }

        protected override void OnStop()
        {
            timer.Enabled = false;
        }

        protected void getTaskIDs()
        {
            Manager sManager = new Manager();
            List<Task> task = sManager.getTaskIds();

            foreach (Task taskId in task)
            {
                String taskid = taskId.TaskID;
                getAppointmentDetails(taskid);

            }
        }

        protected void getAppointmentDetails(string taskid)
        {
            Manager sManager = new Manager();
            List<AppointmentDetails> apDetails = new List<AppointmentDetails>();
            apDetails = sManager.getAppointmentDetails(taskid);

            foreach (AppointmentDetails apDetail in apDetails)
            {
                String subject = apDetail.Subject;
                String description = apDetail.Description;

                Email.EmailSend(subject, description, "preetham.reddy@usairways.com");
            }
        }

        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            getTaskIDs();
                 }


    }
}
Avatar of mattibutt
mattibutt
Flag of United States of America image

if the service you are trying to code has nothing to do then obviously it will stop if you believe the service shouldnt have stopped check the event log to see if any unusual reason for service termination

I would fire off a new thread from within OnStart before returning from it

Make that new thread run indefinately in an endless loop doing the work for your service. I would also use Thread.Sleep instead of a timer to control the work to be done every 5 minutes

Finally make the thread terminate/pause when your service receives the Stop/Pause commands



The On_Start should contain only the Timer.start().

All your jobs needs to be executed on the timer elapsed method.

There is an inner timeout on the On_Start (30 seconds I think).  I had problems before and I recommend only to start the timer in the on_start, then executing everything in the timer_elapsed.

See if it helps you.
Avatar of gautam_reddyc
gautam_reddyc

ASKER


i removed the timeer and used Thread.Sleep

i added the below code in OnStart

while(true)
{
  GetTaskId();
  Thread.Sleep(30000);
}
Still same message.. please help.. let me know if you need more code..
Did you try just starting the timer in OnStart?

Let's say in pseudo  :

Timer = new timer();
Timer.Interval = 300000;
Timer.Enabled = true;

OnStart(){
   Timer.Start();
}
OnStop() {
    Timer.Stop();
}

TimerElapsed(){
  Timer.Stop();
  GetTaskId();
  Timer.Start();
}
No Luck..
Basically i need an Email Remainder function that sends the email 5 minutes before the appointment.

It checks the database and if the appointment is in next five minutes.it sends an email.
Is there any way to do it other than Windows Service...?
ASKER CERTIFIED SOLUTION
Avatar of BurnieP
BurnieP
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial