943,740 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2617
  • C# RSS
Apr 15th, 2008
0

problem with windows service

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  1. public partial class LDMSN : ServiceBase
  2. {
  3. private Timer _timer;
  4. private bool _IsStarted;
  5. private int TimerValue=60000;
  6. public LDMSN()
  7. {
  8. InitializeComponent();
  9. }
  10.  
  11. private void ProcessMessage(object sender, System.Timers.ElapsedEventArgs e)
  12. {
  13. if (_IsStarted)
  14. {
  15. _timer.Stop();
  16. System.ServiceProcess.ServiceController controller = new ServiceController();
  17. controller.MachineName = "."
  18. controller.ServiceName = "MSSQLSERVER";
  19. string status = controller.Status.ToString();
  20. if (status == "started")
  21. {
  22. controller.Stop();
  23. EventLog.WriteEntry("MSSQLSERVER stopped");
  24. }
  25. if (status == "stopped")
  26. {
  27. controller.Start();
  28. EventLog.WriteEntry("MSSQLSERVER started");
  29. }
  30. _timer.Start();
  31. }
  32. }
  33.  
  34. protected override void OnStart(string[] args)
  35. {
  36. EventLog.WriteEntry("LDMSN started");
  37. _timer = new Timer();
  38. _timer.Interval = TimerValue; //every 1 min
  39. _timer.Elapsed += new ElapsedEventHandler(this.ProcessMessage);
  40. }
  41.  
  42. protected override void OnStop()
  43. {
  44. _IsStarted = false;
  45. EventLog.WriteEntry("LDMSN stopped");
  46. }
  47. }
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.
Last edited by Narue; Apr 15th, 2008 at 1:33 pm. Reason: Added code tags, do it yourself next time.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
santoo is offline Offline
17 posts
since Nov 2006
Apr 15th, 2008
1

Re: problem with windows service

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 15th, 2008
0

Re: problem with windows service

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".
Reputation Points: 10
Solved Threads: 0
Newbie Poster
barrydallenbach is offline Offline
4 posts
since Apr 2008
Apr 15th, 2008
0

Re: problem with windows service

Yes you never set "_IsSarted" as well.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
barrydallenbach is offline Offline
4 posts
since Apr 2008
Apr 16th, 2008
0

Re: problem with windows service

C# Syntax (Toggle Plain Text)
  1. public partial class LDMSN : ServiceBase
  2. {
  3. private Timer _timer;
  4. private bool _IsStarted;
  5. private int TimerValue=60000;
  6. public LDMSN()
  7. {
  8. InitializeComponent();
  9. }
  10.  
  11. private void ProcessMessage(object sender, System.Timers.ElapsedEventArgs e)
  12. {
  13. if (_IsStarted)
  14. {
  15. _timer.Stop();
  16. System.ServiceProcess.ServiceController controller = new ServiceController();
  17. controller.MachineName = "."
  18. controller.ServiceName = "MSSQLSERVER";
  19. string status = controller.Status.ToString();
  20. if (status == "started")
  21. {
  22. controller.Stop();
  23. EventLog.WriteEntry("MSSQLSERVER stopped");
  24. }
  25. if (status == "stopped")
  26. {
  27. controller.Start();
  28. EventLog.WriteEntry("MSSQLSERVER started");
  29. }
  30. _timer.Start();
  31. }
  32. }
  33.  
  34. protected override void OnStart(string[] args)
  35. {
  36. EventLog.WriteEntry("LDMSN started");
  37. _timer = new Timer();
  38. _timer.Interval = TimerValue; //every 1 min
  39. _IsStarted = true;
  40. _timer.Elapsed += new ElapsedEventHandler(this.ProcessMessage);
  41. }
  42.  
  43. protected override void OnStop()
  44. {
  45. _IsStarted = false;
  46. EventLog.WriteEntry("LDMSN stopped");
  47. }
  48. }
  49.  
  50. I have set the _IsStarted=true .....also it is not working.
  51.  
  52. I have done a small windows application as below
  53.  
  54. public Form1()
  55. {
  56. InitializeComponent();
  57. controler.MachineName=".";
  58. controler.ServiceName="MSSQLSERVER";
  59. status = controler.Status.ToString();
  60. }
  61.  
  62. private void button1_Click(object sender, EventArgs e)
  63. {
  64. controler.Start();
  65. }
  66.  
  67. private void button2_Click(object sender, EventArgs e)
  68. {
  69. controler.Stop();
  70. }
which is working fine....please tell me how to use timers in windows service.

thanks.
Last edited by Narue; Apr 16th, 2008 at 1:10 pm. Reason: Added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
santoo is offline Offline
17 posts
since Nov 2006
Apr 16th, 2008
0

Re: problem with windows service

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
barrydallenbach is offline Offline
4 posts
since Apr 2008
Apr 17th, 2008
0

Re: problem with windows service

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.
}
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
santoo is offline Offline
17 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Displaying PPT in Webrowser or PicturBox?
Next Thread in C# Forum Timeline: Writing High perfomance server using C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC