Home All Groups Group Topic Archive Search About

How should a Windows Service stop itself?

Author
7 Aug 2006 4:52 AM
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?

Author
7 Aug 2006 5:33 AM
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"?

Show quote
"Dave" <n***@nowhere.com> wrote in message
news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl...
>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?
>
Author
7 Aug 2006 9:42 AM
oscar.acostamontesde@googlemail.com
Hello:
Maybe thowing an exception and returning, but i'm just guessing. Good
luck!
Dave wrote:
Show quote
> 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"?
>
> "Dave" <n***@nowhere.com> wrote in message
> news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl...
> >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?
> >
Author
7 Aug 2006 6:58 PM
Dave
I don't think that will work because it would be thrown from a thread that's
created in OnStart.


<oscar.acostamonte***@googlemail.com> wrote in message
Show quote
news:1154943759.867848.280710@h48g2000cwc.googlegroups.com...
> Hello:
> Maybe thowing an exception and returning, but i'm just guessing. Good
> luck!
> Dave wrote:
>> 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"?
>>
>> "Dave" <n***@nowhere.com> wrote in message
>> news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl...
>> >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?
>> >
>
Author
7 Aug 2006 1:09 PM
Rune Huseby
"Dave" <n***@nowhere.com> wrote in
news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl:

> 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
Author
7 Aug 2006 7:01 PM
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".


Show quote
"Rune Huseby" <rhuse***@broadpark.no.invalid> wrote in message
news:Xns98189A468B484runehusebynewschello@217.13.9.15...
> "Dave" <n***@nowhere.com> wrote in
> news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl:
>
>> 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
Author
8 Aug 2006 7:08 AM
Damien
Dave wrote:
> 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
Author
8 Aug 2006 1:19 PM
Rune Huseby
"Dave" <n***@nowhere.com> wrote in
news:OpWxmPluGHA.1216@TK2MSFTNGP03.phx.gbl:

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





--
Rune Huseby
Author
8 Aug 2006 1:44 PM
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


Show quote
"Rune Huseby" <rhuse***@broadpark.no.invalid> wrote in message
news:Xns98189A468B484runehusebynewschello@217.13.9.15...
| "Dave" <n***@nowhere.com> wrote in
| news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl:
|
| > 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
Author
8 Aug 2006 10:03 PM
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.


Show quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:uRMp9CvuGHA.1772@TK2MSFTNGP06.phx.gbl...
> 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
>
>
> "Rune Huseby" <rhuse***@broadpark.no.invalid> wrote in message
> news:Xns98189A468B484runehusebynewschello@217.13.9.15...
> | "Dave" <n***@nowhere.com> wrote in
> | news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl:
> |
> | > 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
>
>
Author
10 Aug 2006 2:28 AM
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


Show quote
"Dave" <n***@nowhere.com> wrote in message
news:%23XR82ZzuGHA.4436@TK2MSFTNGP05.phx.gbl...
| 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.
|
|
| "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
| message news:uRMp9CvuGHA.1772@TK2MSFTNGP06.phx.gbl...
| > 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
Show quote
| >
| > 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
| >
| >
| > "Rune Huseby" <rhuse***@broadpark.no.invalid> wrote in message
| > news:Xns98189A468B484runehusebynewschello@217.13.9.15...
| > | "Dave" <n***@nowhere.com> wrote in
| > | news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl:
| > |
| > | > 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
| >
| >
|
|
Author
8 Aug 2006 1:47 PM
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/library/system.serviceprocess.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


Show quote
"Dave" <n***@nowhere.com> wrote in message
news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl...
|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?
|
|
Author
8 Aug 2006 10:05 PM
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.


Show quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:e$4zuEvuGHA.4972@TK2MSFTNGP05.phx.gbl...
> 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/library/system.serviceprocess.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
>
>
> "Dave" <n***@nowhere.com> wrote in message
> news:ehu4X1duGHA.4752@TK2MSFTNGP02.phx.gbl...
> |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?
> |
> |
>
>

AddThis Social Bookmark Button