how can a Windows Service stop itself?

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.
 
J

Jerry III

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
 
T

Tao

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.


.
 
D

Dan Schullman

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?
 

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

Top