473,385 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

restart windows service (itself)

Hello,
I would like to know what's the best way to restart one service. I
would like to do it from the service itself. Is this possible?
I try it with

ServiceController.stop()
ServiceController.WaitForStatus(ServiceControllerS tatus.Stopped)
ServiceController.start()

but doesn´t works. It seems waitforstatus instruction is the last
instruction executed.
Thanks in advance
Nov 21 '05 #1
6 3772
Hi there,
you can launch a separate Exe to do the job or even a batch file like
then

Service.exe /stop
Service.exe /start

--
Bishoy Ghaly
Senior Software Developer
bi****@dev2dev.org
http://www.dev2dev.org

------------------------------------------------------

"Leonardo Curros" <jj***@mixmail.com> wrote in message
news:3f**************************@posting.google.c om...
Hello,
I would like to know what's the best way to restart one service. I
would like to do it from the service itself. Is this possible?
I try it with

ServiceController.stop()
ServiceController.WaitForStatus(ServiceControllerS tatus.Stopped)
ServiceController.start()

but doesn´t works. It seems waitforstatus instruction is the last
instruction executed.
Thanks in advance

Nov 21 '05 #2
Leonardo,
A Service can't easily restart itself, as most Windows Services only have a
single Service (a class derived from ServiceBase) in the executable (see
below). As soon as this single Service is stopped the executable is unloaded
from memory, hence your WaitForStatus is the last statement executed. Which
I fine is actually a good thing as it ensures that the entire Service is
flushed and the service's app.config file is reread.

I would consider adding a second service to the same executable, to prevent
the executable from being unloaded, which unfortunately will also prevent
the service's app.config from being reloaded.

Alternatively I would consider creating a second service in a second
executable, that is able to use your statements to restart the first. This
second one I would consider having a timer to periodically check for the
first.

Other possibilities might be to modify the Main routine generated as part of
the Windows Service (in the "Component Designer generated code" region) to
put the ServiceBase.Run statement in a loop to prevent the Main routine from
exiting, hence allowing the executable from unloading. Just remember!
depending on what you do in the Main routine, it may prevent your executable
from being unloaded!

Hope this helps
Jay

"Leonardo Curros" <jj***@mixmail.com> wrote in message
news:3f**************************@posting.google.c om...
Hello,
I would like to know what's the best way to restart one service. I
would like to do it from the service itself. Is this possible?
I try it with

ServiceController.stop()
ServiceController.WaitForStatus(ServiceControllerS tatus.Stopped)
ServiceController.start()

but doesn´t works. It seems waitforstatus instruction is the last
instruction executed.
Thanks in advance

Nov 21 '05 #3
Thanks. But I would like to restart it only in special situations.
I try to restart it launching one separated thread with code

controller.Stop()
controller.WaitForStatus(ServiceControllerStatus.S topped)
controller.Start()

and apparently works perfectly. Is this ok?

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:<OI**************@TK2MSFTNGP14.phx.gbl>...
Leonardo,
A Service can't easily restart itself, as most Windows Services only have a
single Service (a class derived from ServiceBase) in the executable (see
below). As soon as this single Service is stopped the executable is unloaded
from memory, hence your WaitForStatus is the last statement executed. Which
I fine is actually a good thing as it ensures that the entire Service is
flushed and the service's app.config file is reread.

I would consider adding a second service to the same executable, to prevent
the executable from being unloaded, which unfortunately will also prevent
the service's app.config from being reloaded.

Alternatively I would consider creating a second service in a second
executable, that is able to use your statements to restart the first. This
second one I would consider having a timer to periodically check for the
first.

Other possibilities might be to modify the Main routine generated as part of
the Windows Service (in the "Component Designer generated code" region) to
put the ServiceBase.Run statement in a loop to prevent the Main routine from
exiting, hence allowing the executable from unloading. Just remember!
depending on what you do in the Main routine, it may prevent your executable
from being unloaded!

Hope this helps
Jay

"Leonardo Curros" <jj***@mixmail.com> wrote in message
news:3f**************************@posting.google.c om...
Hello,
I would like to know what's the best way to restart one service. I
would like to do it from the service itself. Is this possible?
I try it with

ServiceController.stop()
ServiceController.WaitForStatus(ServiceControllerS tatus.Stopped)
ServiceController.start()

but doesn´t works. It seems waitforstatus instruction is the last
instruction executed.
Thanks in advance

Nov 21 '05 #4
Thanks. But I would like to restart it only in special situations.
I try to restart it launching one separated thread with code

controller.Stop()
controller.WaitForStatus(ServiceControllerStatus.S topped)
controller.Start()

and apparently works perfectly. Is this ok?

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:<OI**************@TK2MSFTNGP14.phx.gbl>...
Leonardo,
A Service can't easily restart itself, as most Windows Services only have a
single Service (a class derived from ServiceBase) in the executable (see
below). As soon as this single Service is stopped the executable is unloaded
from memory, hence your WaitForStatus is the last statement executed. Which
I fine is actually a good thing as it ensures that the entire Service is
flushed and the service's app.config file is reread.

I would consider adding a second service to the same executable, to prevent
the executable from being unloaded, which unfortunately will also prevent
the service's app.config from being reloaded.

Alternatively I would consider creating a second service in a second
executable, that is able to use your statements to restart the first. This
second one I would consider having a timer to periodically check for the
first.

Other possibilities might be to modify the Main routine generated as part of
the Windows Service (in the "Component Designer generated code" region) to
put the ServiceBase.Run statement in a loop to prevent the Main routine from
exiting, hence allowing the executable from unloading. Just remember!
depending on what you do in the Main routine, it may prevent your executable
from being unloaded!

Hope this helps
Jay

"Leonardo Curros" <jj***@mixmail.com> wrote in message
news:3f**************************@posting.google.c om...
Hello,
I would like to know what's the best way to restart one service. I
would like to do it from the service itself. Is this possible?
I try it with

ServiceController.stop()
ServiceController.WaitForStatus(ServiceControllerS tatus.Stopped)
ServiceController.start()

but doesn´t works. It seems waitforstatus instruction is the last
instruction executed.
Thanks in advance

Nov 21 '05 #5
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
"Leonardo Curros" <jj***@mixmail.com> wrote in message
news:3f**************************@posting.google.c om... Thanks. But I would like to restart it only in special situations.
I try to restart it launching one separated thread with code

controller.Stop()
controller.WaitForStatus(ServiceControllerStatus.S topped)
controller.Start()

and apparently works perfectly. Is this ok?

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:<OI**************@TK2MSFTNGP14.phx.gbl>...
Leonardo,
A Service can't easily restart itself, as most Windows Services only have
a
single Service (a class derived from ServiceBase) in the executable (see
below). As soon as this single Service is stopped the executable is
unloaded
from memory, hence your WaitForStatus is the last statement executed.
Which
I fine is actually a good thing as it ensures that the entire Service is
flushed and the service's app.config file is reread.

I would consider adding a second service to the same executable, to
prevent
the executable from being unloaded, which unfortunately will also prevent
the service's app.config from being reloaded.

Alternatively I would consider creating a second service in a second
executable, that is able to use your statements to restart the first.
This
second one I would consider having a timer to periodically check for the
first.

Other possibilities might be to modify the Main routine generated as part
of
the Windows Service (in the "Component Designer generated code" region)
to
put the ServiceBase.Run statement in a loop to prevent the Main routine
from
exiting, hence allowing the executable from unloading. Just remember!
depending on what you do in the Main routine, it may prevent your
executable
from being unloaded!

Hope this helps
Jay

"Leonardo Curros" <jj***@mixmail.com> wrote in message
news:3f**************************@posting.google.c om...
> Hello,
> I would like to know what's the best way to restart one service. I
> would like to do it from the service itself. Is this possible?
> I try it with
>
> ServiceController.stop()
> ServiceController.WaitForStatus(ServiceControllerS tatus.Stopped)
> ServiceController.start()
>
> but doesn´t works. It seems waitforstatus instruction is the last
> instruction executed.
>
>
> Thanks in advance

Nov 21 '05 #6
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
"Leonardo Curros" <jj***@mixmail.com> wrote in message
news:3f**************************@posting.google.c om... Thanks. But I would like to restart it only in special situations.
I try to restart it launching one separated thread with code

controller.Stop()
controller.WaitForStatus(ServiceControllerStatus.S topped)
controller.Start()

and apparently works perfectly. Is this ok?

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:<OI**************@TK2MSFTNGP14.phx.gbl>...
Leonardo,
A Service can't easily restart itself, as most Windows Services only have
a
single Service (a class derived from ServiceBase) in the executable (see
below). As soon as this single Service is stopped the executable is
unloaded
from memory, hence your WaitForStatus is the last statement executed.
Which
I fine is actually a good thing as it ensures that the entire Service is
flushed and the service's app.config file is reread.

I would consider adding a second service to the same executable, to
prevent
the executable from being unloaded, which unfortunately will also prevent
the service's app.config from being reloaded.

Alternatively I would consider creating a second service in a second
executable, that is able to use your statements to restart the first.
This
second one I would consider having a timer to periodically check for the
first.

Other possibilities might be to modify the Main routine generated as part
of
the Windows Service (in the "Component Designer generated code" region)
to
put the ServiceBase.Run statement in a loop to prevent the Main routine
from
exiting, hence allowing the executable from unloading. Just remember!
depending on what you do in the Main routine, it may prevent your
executable
from being unloaded!

Hope this helps
Jay

"Leonardo Curros" <jj***@mixmail.com> wrote in message
news:3f**************************@posting.google.c om...
> Hello,
> I would like to know what's the best way to restart one service. I
> would like to do it from the service itself. Is this possible?
> I try it with
>
> ServiceController.stop()
> ServiceController.WaitForStatus(ServiceControllerS tatus.Stopped)
> ServiceController.start()
>
> but doesn´t works. It seems waitforstatus instruction is the last
> instruction executed.
>
>
> Thanks in advance

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paolo Veronelli | last post by:
I want to add a sensible address to my nevow server, so it has to launch an executable that stop itself and restart it. I tried os.execl('restart','') but I have a format error and I do not...
2
by: Chris Langston | last post by:
I have a Web Server running IIS 5 or 6 on Windows 2K and Windows 2003 Server that is experiencing strange shutdown problems. We are using ASP.NET v1.1 and our application is written in VB.NET ...
14
by: iceman | last post by:
Hello, I have a windows service. I want to restart it after every 24 hour. Is it possible to restart the service programmatically(from the service itself) using the sercvice controller object?...
0
by: felecha | last post by:
Is there a way to write a Service so that it will fail? I'm working on a Service written in VB.Net, that supports an application we are building, and I want to make sure that it can restart...
3
by: Matt | last post by:
Is there an 'easy' way to have a service stop and restart itself? I currently have a seperate service running who's job is to watch the other service and restart it if there is a problem... can I...
2
by: Tedmond | last post by:
Dear all, I wrote a service by C# but I found the service is not quite stable. I want to add some code that refresh or restart the service itself for a fixed period. Does anyone know how to...
1
by: Sergey Krutous | last post by:
Can you please help me to resolve the following issue: I developed a windows service. At start up (I have overriden OnStart method) it connects to a web service and if the connection fails the...
3
by: wwwmike | last post by:
Once a while my server is showing my the "Service Unavailable" page when IIS6 kills itself. My shared hosting at allows me to click on an "Update Website" button which restarts the whole site. ...
8
by: =?Utf-8?B?TWlrZVo=?= | last post by:
How Can Windows Service Application Reatarts itself? Thanks.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.