Hi,

I am sorry if I am posting ...at wrong place.I donno where to post this.

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.

>it is just writing into log entry
Is it also writing the ProcessMessage log entries? It looks to me like you'll never get inside the outer if statement in ProcessMessage because you never set _IsStarted to true, and the default value is false.

Why would you want to start and stop the SQLSERVER every 30 Minutes?

Look more at your use of serviceControler.
You need to use getservices() to return an array of current process. Then search the array list looking for the "SQLSERVER".

Yes you never set "_IsSarted" as well.

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
            _IsStarted = true;
            _timer.Elapsed += new ElapsedEventHandler(this.ProcessMessage);
        }

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

I have set the _IsStarted=true .....also it is not working.

I have done a small windows application as below

 public Form1()
        {
            InitializeComponent();
            controler.MachineName=".";
            controler.ServiceName="MSSQLSERVER";
            status  = controler.Status.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            controler.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            controler.Stop();
        }

which is working fine....please tell me how to use timers in windows service.

thanks.

are you using the System.Timers.Timer object? You did not show the "Using" statements at the top of your program.
You need to use the System.Timers.Timer in a service.
Looking at your code you did not enable the timer. At the end of OnStart add a line that has _timer.Start();
and in the processMessage function you should always _timer.Stop() at the beginning of function and restart at the end. Take the stop and start out of the if sections.

Hi...this is the complete code.I had set the timer for 1min.When I start the service it is working fine for about 3 or 4 min..and it stopps working.what is the reason.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Configuration;


namespace LDMSN
{
public partial class LDMSN : ServiceBase
{
private string IP;
public LDMSN()
{
InitializeComponent();
IP = ConfigurationManager.AppSettings.Get("IP");//IP="machine name"
}


public void EventAction(object sender)
{
string PcName = IP;
ServiceController[] services = ServiceController.GetServices();
for (int i = 0; i < services.Length; i++)
{


if (services.DisplayName == "MSSQLSERVER")
{
ServiceController sc = new ServiceController(services.DisplayName, PcName);
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}


}
}
System.IO.File.AppendAllText("C:\\AuthorLog.txt","AuthorLogService fires EventAction at " + System.DateTime.Now.ToString());
}


protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
const int timervalue = 60000;
System.Threading.Timer Timer = null;


System.IO.File.AppendAllText("C:\\AuthorLog.txt","AuthorLogService has been started at " + System.DateTime.Now.ToString());


System.Threading.TimerCallback tDelegate = new System.Threading.TimerCallback(EventAction);
Timer = new System.Threading.Timer(tDelegate, this, 0, timervalue);
}


protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.