How should a Windows Service stop itself?

D

Dave

I have written a multi-threaded Windows Service in .NET. During startup of
the service I need to check for some condition and stop the service if the
condition is not met. What is the best way to do that? Due to time
constraints I have to check the condition in a separate thread created in
the OnStart event so that the OnStart event can complete within the 30
second window that Windows requires for service start.

I assume the only way for a service to stop itself is to use the service
controller class?
 
D

Dave

One more thing: if the condition is not met and the service needs to stop
itself I want to make Windows think the service "failed" so that the
service's Recovery option can be configured to automatically restart the
service or do the other things that the Recovery option allows. If I just
use the service controller class to stop the service then Windows doesn't
count that as a failure and won't use the Recovery options. How do I
programmatically make the service "fail"?
 
O

oscar.acostamontesde

Hello:
Maybe thowing an exception and returning, but i'm just guessing. Good
luck!
 
R

Rune Huseby

I have written a multi-threaded Windows Service in .NET. During
startup of the service I need to check for some condition and stop the
service if the condition is not met. What is the best way to do that?
Due to time constraints I have to check the condition in a separate
thread created in the OnStart event so that the OnStart event can
complete within the 30 second window that Windows requires for service
start.

I assume the only way for a service to stop itself is to use the
service controller class?

That is correct, you use ServiceController.Stop. But you cannot do this in
the OnStart-event. If you need to stop before OnStart is done, start an
async. thread, ie. using a Timer, call ServiceController.Stop from there.
 
D

Dave

I don't think that will work because it would be thrown from a thread that's
created in OnStart.
 
D

Dave

ServiceController.Stop works ok called from a separate thread started in
OnStart event. The problem is I besides stopping the service I would really
like the programmatically "fail" it so that the Windows Service Recovery
options can be used by the user to restart the service, etc. But I don't
know how to make it "fail".
 
D

Damien

Dave said:
ServiceController.Stop works ok called from a separate thread started in
OnStart event. The problem is I besides stopping the service I would really
like the programmatically "fail" it so that the Windows Service Recovery
options can be used by the user to restart the service, etc. But I don't
know how to make it "fail".
Just a wild guess, but would System.Environment.Exit(-1) work? I've not
had to do this myself, but a wild guess is that failure is based on the
return code from the process.

In fact, I was feeling kind, so I knocked together a service which
kicked off a thread which slept for thirty seconds then made the above
call. I set a failure action (caused it to "net send" a failure message
to me, and it worked, so that's what I'd recommend.

Damien
 
R

Rune Huseby

ServiceController.Stop works ok called from a separate thread started
in OnStart event. The problem is I besides stopping the service I
would really like the programmatically "fail" it so that the Windows
Service Recovery options can be used by the user to restart the
service, etc. But I don't know how to make it "fail".

Hm, you say it takes to much time to find out in OnStart if you should
fail? Maybe you have to go below .NET and use the Win32-equivalents. Ie.
you can use the Win32-api SetServiceStatus to give hints to windows that
you take longer than the expected 30 seconds to start.
 
J

Jay B. Harlow [MVP - Outlook]

Rune,
| If you need to stop before OnStart is done, start an
| async. thread, ie. using a Timer, call ServiceController.Stop from there.
If you need to stop before OnStart is done, simply call ServiceBase.Stop.

http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.stop.aspx

If you are running .NET 1.x, I would first recommend upgrading, second
consider using the Environment.Exit hack, third consider using the async
thread hack

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| |
| > I have written a multi-threaded Windows Service in .NET. During
| > startup of the service I need to check for some condition and stop the
| > service if the condition is not met. What is the best way to do that?
| > Due to time constraints I have to check the condition in a separate
| > thread created in the OnStart event so that the OnStart event can
| > complete within the 30 second window that Windows requires for service
| > start.
| >
| > I assume the only way for a service to stop itself is to use the
| > service controller class?
|
| That is correct, you use ServiceController.Stop. But you cannot do this in
| the OnStart-event. If you need to stop before OnStart is done, start an
| async. thread, ie. using a Timer, call ServiceController.Stop from there.
|
| --
| Rune Huseby
 
J

Jay B. Harlow [MVP - Outlook]

Dave,
| Due to time
| constraints I have to check the condition in a separate thread created in
| the OnStart event so that the OnStart event can complete within the 30
I would consider using ServiceBase.RequestAdditionalTime instead of doing
the startup in an alternate thread.

http://msdn2.microsoft.com/en-us/li...rocess.servicebase.requestadditionaltime.aspx

This way if the startup failed, you can simply throw an exception to
indication the service failed...

| I assume the only way for a service to stop itself is to use the service
| controller class?
There is also ServiceBase.Stop & ServiceBase.ExitCode

http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.stop.aspx

http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.exitcode.aspx

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I have written a multi-threaded Windows Service in .NET. During startup of
| the service I need to check for some condition and stop the service if the
| condition is not met. What is the best way to do that? Due to time
| constraints I have to check the condition in a separate thread created in
| the OnStart event so that the OnStart event can complete within the 30
| second window that Windows requires for service start.
|
| I assume the only way for a service to stop itself is to use the service
| controller class?
|
|
 
D

Dave

ServiceBase.Stop() isn't new in .NET 2.0.

I'm going to try Environment.Exit. That's what I was looking for, I just
didn't know what it was called.
 
D

Dave

I didn't know about RequestAdditionalTime. That's interesting. Looks like
it's only in .NET 2.0 though and I'm still using 1.1 for now.
 
J

Jay B. Harlow [MVP - Outlook]

Dave,
| ServiceBase.Stop() isn't new in .NET 2.0.
Did you mean it *is* new in .NET 2.0?

As the page I gave suggests its new to .NET 2.0, its not available in .NET
1.x.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| ServiceBase.Stop() isn't new in .NET 2.0.
|
| I'm going to try Environment.Exit. That's what I was looking for, I just
| didn't know what it was called.
|
|
| message | > Rune,
| > | If you need to stop before OnStart is done, start an
| > | async. thread, ie. using a Timer, call ServiceController.Stop from
| > there.
| > If you need to stop before OnStart is done, simply call
ServiceBase.Stop.
| >
| >
http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.stop.aspx
| >
| > If you are running .NET 1.x, I would first recommend upgrading, second
| > consider using the Environment.Exit hack, third consider using the async
| > thread hack
| >
| > --
| > Hope this helps
| > Jay B. Harlow [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > | | > |
| > | > I have written a multi-threaded Windows Service in .NET. During
| > | > startup of the service I need to check for some condition and stop
the
| > | > service if the condition is not met. What is the best way to do
that?
| > | > Due to time constraints I have to check the condition in a
separate
| > | > thread created in the OnStart event so that the OnStart event can
| > | > complete within the 30 second window that Windows requires for
service
| > | > start.
| > | >
| > | > I assume the only way for a service to stop itself is to use the
| > | > service controller class?
| > |
| > | That is correct, you use ServiceController.Stop. But you cannot do
this
| > in
| > | the OnStart-event. If you need to stop before OnStart is done, start
an
| > | async. thread, ie. using a Timer, call ServiceController.Stop from
| > there.
| > |
| > | --
| > | Rune Huseby
| >
| >
|
|
 

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