•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 401,468 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,032 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 750 | Replies: 6
![]() |
•
•
Join Date: Nov 2006
Posts: 17
Reputation:
Rep Power: 2
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:
csharp 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 12:33 pm. Reason: Added code tags, do it yourself next time.
>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.
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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Nov 2006
Posts: 17
Reputation:
Rep Power: 2
Solved Threads: 0
csharp 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 12:10 pm. Reason: Added code tags
•
•
Join Date: Apr 2008
Location: Nothern Idaho
Posts: 4
Reputation:
Rep Power: 0
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:
Rep Power: 2
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.
}
}
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
apple browser choose computer crack cracked defender dell development fiji gates google hack install internet leopard linux mac management melinda merger microsoft mobile news office open operating os photo pirate research safari security server service software source sp1 spyware survey system ubuntu unix upgrade vista windows windows update windows vista xp yahoo
- Previous Thread: Displaying PPT in Webrowser or PicturBox?
- Next Thread: Writing High perfomance server using C#



Linear Mode