2
Reply

problem with windows service

ranjith n

ranjith n

Apr 15 2008 10:20 AM
2.7k

 Hi,

I have developed a windows service which uses a timer...to run the SQL Server for exactly 30min.If sqlserer service is stopped it starts the service and stops it after 30min and vice versa.

I have written the following code:

public partial class LDMSN : ServiceBase
    {
        private Timer _timer;
        private bool _IsStarted;
        private int TimerValue=60000;
        public LDMSN()
        {
            InitializeComponent();
        }

        private void ProcessMessage(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (_IsStarted)
            {
                _timer.Stop();
              System.ServiceProcess.ServiceController controller = new ServiceController();
                controller.MachineName = "."
                controller.ServiceName = "MSSQLSERVER";
                string status = controller.Status.ToString();
                if (status == "started")
                {
                    controller.Stop();
                    EventLog.WriteEntry("MSSQLSERVER stopped");
                }
                if (status == "stopped")
                {
                    controller.Start();
                    EventLog.WriteEntry("MSSQLSERVER started");
                }
                _timer.Start();
            }
        }

        protected override void OnStart(string[] args)
        {
            EventLog.WriteEntry("LDMSN started");
            _timer = new Timer();
            _timer.Interval = TimerValue; //every 1 min
            _timer.Elapsed += new ElapsedEventHandler(this.ProcessMessage);
        }

        protected override void OnStop()
        {
            _IsStarted = false;
            EventLog.WriteEntry("LDMSN stopped");
        }
    }

But the service is not working..it is just writing into log entry.But not doing what it is supposed to do.please help me regarding this..

thanks. 

 


Answers (2)