problem with windows service

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 17
Reputation: santoo is an unknown quantity at this point 
Solved Threads: 0
santoo santoo is offline Offline
Newbie Poster

problem with windows service

 
0
  #1
Apr 15th, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,730
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem with windows service

 
1
  #2
Apr 15th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 4
Reputation: barrydallenbach is an unknown quantity at this point 
Solved Threads: 0
barrydallenbach barrydallenbach is offline Offline
Newbie Poster

Re: problem with windows service

 
0
  #3
Apr 15th, 2008
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".
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 4
Reputation: barrydallenbach is an unknown quantity at this point 
Solved Threads: 0
barrydallenbach barrydallenbach is offline Offline
Newbie Poster

Re: problem with windows service

 
0
  #4
Apr 15th, 2008
Yes you never set "_IsSarted" as well.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 17
Reputation: santoo is an unknown quantity at this point 
Solved Threads: 0
santoo santoo is offline Offline
Newbie Poster

Re: problem with windows service

 
0
  #5
Apr 16th, 2008
  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 4
Reputation: barrydallenbach is an unknown quantity at this point 
Solved Threads: 0
barrydallenbach barrydallenbach is offline Offline
Newbie Poster

Re: problem with windows service

 
0
  #6
Apr 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 17
Reputation: santoo is an unknown quantity at this point 
Solved Threads: 0
santoo santoo is offline Offline
Newbie Poster

Re: problem with windows service

 
0
  #7
Apr 17th, 2008
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.
}
}
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC