| | |
problem with windows service
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 17
Reputation:
Solved Threads: 0
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:
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.
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:
C# Syntax (Toggle Plain Text)
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"); } }
thanks.
Last edited by Narue; Apr 15th, 2008 at 1:33 pm. Reason: Added code tags, do it yourself next time.
•
•
Join Date: Nov 2006
Posts: 17
Reputation:
Solved Threads: 0
C# Syntax (Toggle Plain Text)
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(); }
thanks.
Last edited by Narue; Apr 16th, 2008 at 1:10 pm. Reason: Added code tags
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Nov 2006
Posts: 17
Reputation:
Solved Threads: 0
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[i].DisplayName == "MSSQLSERVER")
{
ServiceController sc = new ServiceController(services[i].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.
}
}
}
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[i].DisplayName == "MSSQLSERVER")
{
ServiceController sc = new ServiceController(services[i].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.
}
}
}
![]() |
Other Threads in the C# Forum
- Previous Thread: Displaying PPT in Webrowser or PicturBox?
- Next Thread: Writing High perfomance server using C#
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# capturing check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drag draganddrop drawing encryption enum error event excel file files firefox form format forms function gdi+ httpwebrequest image index input install java label libraries list listbox listener loop mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion port post programming radians regex remote remoting richtextbox running... saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






