Windows Service and C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anyone tell how to stop a service from within the service,

example:

On starting the service needs a file from the disk if it does not find the
file i need the service stop and end. But I can’t find any command or methods
for stopping the service from running any further.

can anyone help?
 
Just throw an (unhandled) exception and your service will stop.

Willy.
 
You hacker Willy............lol

ServiceController serviceMonitor = new ServiceController("servicename");
if( serviceMonitor == ServiceControllerStatus.Running )
{
serviceMonitor.Stop();
}

However, I have seen this justifiably fail when trying to stop the service
from within itself ..........ideally you should stop the service from a
controlling service by having your service cleanly set an external param
that causes the service to be terminated by the controller. In reality a
service should not die but terminate cleanly by timed or user action which
is why I'm suggesting you should use a controller, your service should
remain active even if the files not there and handle the
exception............Willy's suggestion will undoutably work though.
--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top