Prevent a service from stopping.

  • Thread starter Thread starter chris.hearson
  • Start date Start date
C

chris.hearson

How do I programmatically prevent a service from stopping? I want to
be able to keep my service running (started), under certain conditions,
when a user tries to stop it. I have tried throwing an exception

- from OnStop(): the SCM waits for a long time, reports that the
service didn't respond in a timely fashion and then leaves the status
as stopping.
- from Dispose(): the SCM displays an error dialog with the exception
details and prompts me to select a debugger. If I select 'No' the
service is stopped.

..NET Framework 1.1
MSDE 2003
Windows 2000 SP4
 
Set the 'CanStop' property of the service to false.

-mdb

(e-mail address removed) wrote in @g47g2000cwa.googlegroups.com:
 
Set the 'CanStop' property of the service to false.

Sorry one thing I didn't notice was that you said you only want to prevent
the service from stopping under certain conditions. If that's the case,
setting the CanStop property to false wouldn't quite suffice.

Here's a further suggestion. Set the CanStop property to false, as above.
But then you will need an application that the user can use to request a
stop. This app would then communicate with your service (via remoting,
perhaps) and if the condition is permitted, stop itself. You should be
able to do this by dynamically setting 'CanStop' to true, then connecting
to yourself with the ServiceController class and issuing the 'Stop'
command.

I haven't actually tried it, but I think that would work.

-mdb
 
Just s quick note - you wouldn't need remoting for controlling the service,
a ServiceController's ExecuteCommand method would suffice if you need just a
parameterless command not returning anything
 
Just s quick note - you wouldn't need remoting for controlling the
service, a ServiceController's ExecuteCommand method would suffice if
you need just a parameterless command not returning anything

I've tried using the ExecuteCommand before but I couldn't get it to work.
I forget what the specific error was. Any tricks?

-mdb
 
You will have to specify "the specific error" This can be caused by numerous
reasons, insufficient privileges to control the service would be the first
thing to look at.
 

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