troubleshooting Question

Windows Service Starts and Stops Automatically

Avatar of gautam_reddyc
gautam_reddyc asked on
C#
7 Comments1 Solution317 ViewsLast Modified:
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();
                 }


    }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros