Click here to Skip to main content
16,023,047 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have made a window service and deployed it on my PC and need to check whether the service is running or not on every 15 min. Here is my code:


protected override void OnStart(string[] args)
{
WindowServicesStarted();
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Interval = 900000;//15 Min 900000
timer.Enabled = true;
}
if (service.Status == ServiceControllerStatus.Stopped)
{
// this.timer.Stop();
this.timer.Enabled = false;
service.Start();
this.timer.Enabled = true;
//this.timer.Start();
}

but i am unable to start the service as error comes like

cannot start the service on machine ".".

can anybody help me in it as it is very urgent.

Thanks in adv.

Gaurav Sharma
Posted

If your service has stopped, none of the timer code will be running. If you want to do that, you're going to have to write a desktop (systray) application that performs the act of restarting your service.

The bigger quetion is this - why did your service stop in the first place? An exception? Nothing to do?

In your service, you have to have some reason for it to run, like a timer loop (ugh) or some sort of keep-alive thread.
 
Share this answer
 
The code you have posted doesn't make a lot of sense, what does WindowServicesStarted() do, also I take it that the if statement is actually in the event handler for the timer.

What you are trying to do is pointless, because if the service has stopped then the timer will have also stopped and so will not fire its event. It's the whole point of a stopped service is that the code is no longer running.
 
Share this answer
 
I had also used timer control in my windows service and facing the same problem. I had used threading in place of Timer control and it was working fine.

Timer control may create problem in windows service. You can apply threading in windows service rather than Timer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900