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.
ranjith nPosted Apr 15, 2008, 11:53 PM
But it is not working...can u please help me...where should I write the code for doing the actual process...starting or stopping the SQLSERVER .
I have written a small windows application
public partial class Form1 : Form
{
ServiceController controler = new ServiceController();
private string status;
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...it is starting and stopping the sqlserver service.
thanks.
AlanPosted Apr 15, 2008, 2:09 PM