how can a Windows Service stop itself?

  • Thread starter Thread starter Dan Schullman
  • Start date Start date
D

Dan Schullman

Hello,

How can a Windows Service abort/stop itself after it has returned from
OnStart? That is, after a service has successfully started, it later
determines that it needs to stop. How can it make the Service Control
Manager aware that it has stopped (or wants to stop)?

In a single-service process, it appears that simply exiting the process is
sufficient to let the Service Control Manager know that the service is no
longer running. But this seems rather dirty AND doesn't handle the case of
a multiple-service process.

[I'm assuming that it is NOT legit to use ServiceController.stop() to stop
oneself.]

Thanks,
Dan S.
 
In Win32 API you would use SetServiceStatus to notify the service control
manager that you stopped. I don't know how to do this in .Net but I don't
know why you couldn't call Stop on yourself.

Jerry
 
Just throw an exception when you want to stop the service,
but the urgy is it will raise a message with an un-clear
message.
-----Original Message-----
Hello,

How can a Windows Service abort/stop itself after it has returned from
OnStart? That is, after a service has successfully started, it later
determines that it needs to stop. How can it make the Service Control
Manager aware that it has stopped (or wants to stop)?

In a single-service process, it appears that simply exiting the process is
sufficient to let the Service Control Manager know that the service is no
longer running. But this seems rather dirty AND doesn't handle the case of
a multiple-service process.

[I'm assuming that it is NOT legit to use
ServiceController.stop() to stop
oneself.]

Thanks,
Dan S.


.
 
But what if I've got a multi-service process? Will throwing an exception
cause the Service Control Manager to think that all of them have aborted, or
only the one that corresponds to the currently executing thread?
 
Back
Top