Leonardo,
Thanks. But I would like to restart it only in special situations.
I fully understand that.
When I need a service to "restart" itself, and its not because of app.config
reload, rather then use code such as yours to physically restart the service
I simply call a routine that resets my class variables. Alternatively you
could have a separate object for the real service, and your ServiceBase
class simply starts this separate object in a new AppDomain. If you need to
"restart" the ServiceBase class simply creates a new AppDomain containing a
new instance of this separate object... Using an AppDomain also causes the
app.config to reload. However its a little more work to get correct.
Alternatively if you have a separate object for the real service, you could
simply instantiate it without using an AppDomain. The AppDomain offers an
extra layer of isolation.
I do use code similar to yours when the Windows App that controls the
service needs or wants to restart the service, such as updating the
service's app.config with new parameters...
and apparently works perfectly. Is this ok?
Does it work?
Does it work reliably?
If it works & works reliably then it should be Ok.
However! My understanding is that the ServiceBase.Run will exit if all the
services that it was called with are stopped. If my understanding is
correct, your code would not work reliably, as ServiceBase.Run has exited.
However depending on how you started the second thread running (New Thread,
Thread.Background = False) the runtime will not allow your executable to
exit, keeping the service alive. My concern is your service may now be in
unstable territory...
I think my hypotheses really depends if ServiceBase.Run is implemented like
Application.Run or if it registers a call back that the Service Control
Manager (SCM, Win32 component that manages services) calls into. I really
don't know how the SCM sends the different commands to the individual
services. Note to self: what ServiceBase.Run is really doing may be a detail
that is worth investigating...
Hope this helps
Jay