User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 423,734 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,368 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.
Please support our C# advertiser: Programming Forums
Views: 842 | Replies: 6
Reply
Join Date: Nov 2006
Posts: 17
Reputation: santoo is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
santoo santoo is offline Offline
Newbie Poster

problem with windows service

  #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 12:33 pm. Reason: Added code tags, do it yourself next time.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,306
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: problem with windows service

  #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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Apr 2008
Location: Nothern Idaho
Posts: 4
Reputation: barrydallenbach is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
barrydallenbach barrydallenbach is offline Offline
Newbie Poster

Re: problem with windows service

  #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  
Join Date: Apr 2008
Location: Nothern Idaho
Posts: 4
Reputation: barrydallenbach is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
barrydallenbach barrydallenbach is offline Offline
Newbie Poster

Re: problem with windows service

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

Re: problem with windows service

  #5  
Apr 15th, 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 12:10 pm. Reason: Added code tags
Reply With Quote  
Join Date: Apr 2008
Location: Nothern Idaho
Posts: 4
Reputation: barrydallenbach is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
barrydallenbach barrydallenbach is offline Offline
Newbie Poster

Re: problem with windows service

  #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  
Join Date: Nov 2006
Posts: 17
Reputation: santoo is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
santoo santoo is offline Offline
Newbie Poster

Re: problem with windows service

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C# Forum

All times are GMT -4. The time now is 1:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC