473,883 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

ServiceControll er.stop()
ServiceControll er.WaitForStatu s(ServiceContro llerStatus.Stop ped)
ServiceControll er.start()

but doesn´t works. It seems waitforstatus instruction is the last
instruction executed.
Thanks in advance
Nov 21 '05 #1
6 3811
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.goo gle.com...
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

ServiceControll er.stop()
ServiceControll er.WaitForStatu s(ServiceContro llerStatus.Stop ped)
ServiceControll er.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.goo gle.com...
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

ServiceControll er.stop()
ServiceControll er.WaitForStatu s(ServiceContro llerStatus.Stop ped)
ServiceControll er.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.Wait ForStatus(Servi ceControllerSta tus.Stopped)
controller.Star t()

and apparently works perfectly. Is this ok?

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:<OI******* *******@TK2MSFT NGP14.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.goo gle.com...
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

ServiceControll er.stop()
ServiceControll er.WaitForStatu s(ServiceContro llerStatus.Stop ped)
ServiceControll er.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.Wait ForStatus(Servi ceControllerSta tus.Stopped)
controller.Star t()

and apparently works perfectly. Is this ok?

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:<OI******* *******@TK2MSFT NGP14.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.goo gle.com...
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

ServiceControll er.stop()
ServiceControll er.WaitForStatu s(ServiceContro llerStatus.Stop ped)
ServiceControll er.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.Backgrou nd = 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.goo gle.com... 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.Wait ForStatus(Servi ceControllerSta tus.Stopped)
controller.Star t()

and apparently works perfectly. Is this ok?

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:<OI******* *******@TK2MSFT NGP14.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.goo gle.com...
> 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
>
> ServiceControll er.stop()
> ServiceControll er.WaitForStatu s(ServiceContro llerStatus.Stop ped)
> ServiceControll er.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.Backgrou nd = 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.goo gle.com... 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.Wait ForStatus(Servi ceControllerSta tus.Stopped)
controller.Star t()

and apparently works perfectly. Is this ok?

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:<OI******* *******@TK2MSFT NGP14.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.goo gle.com...
> 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
>
> ServiceControll er.stop()
> ServiceControll er.WaitForStatu s(ServiceContro llerStatus.Stop ped)
> ServiceControll er.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
1939
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 understand the manual page. thanks for help
2
4598
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 Here's the scenario: 1. .NET Windows Client on a remote machine makes a web service call to update tables on a Web Server running SQL Server 2000. 2. The Update is updating about 1000 - 3000 records doing simple update statements like "Update...
14
25136
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? Or should i chose some other approach? Any ideas? Thanx for all help. - Iceman
0
1496
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 itself. This is my first experience with it. In the Service Control Manager I know there are the options to say what to do if it fails, obviously I want to select Restart the Service. But how to test it? I can kill the Service there, but it does...
3
2301
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 eliminate that one and have the service restart itself? Thanks, Matt
2
3540
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 programmatically restart a service itself? Or I need a separate process to restart my service? Thanks for any help. Tedmond
1
3706
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 windows service sets ExitCode property to non-zero value (55) and calls Stop method. If this happens when user starts the service in Services snap-in SCM shows error massage and service stops - it's OK. I configured automatic startup and Recovery...
3
1426
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. The process to restart my page takes between 4 to over 6 hours! My hoster told me that I have about 200,000 files in a directory folder and when the site restarts, windows loops trough each file to set security properties (NTFS). The whole thing...
8
9833
by: =?Utf-8?B?TWlrZVo=?= | last post by:
How Can Windows Service Application Reatarts itself? Thanks.
0
9793
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10750
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10419
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3237
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.