|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How should a Windows Service stop itself?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? 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? > 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? > > 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? >> > > "Dave" <n***@nowhere.com> wrote in That is correct, you use ServiceController.Stop. But you cannot do this 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? 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 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 Dave wrote:
> ServiceController.Stop works ok called from a separate thread started in Just a wild guess, but would System.Environment.Exit(-1) work? I've not> 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". > 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 "Dave" <n***@nowhere.com> wrote in Hm, you say it takes to much time to find out in OnStart if you should 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". 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 Rune,
| If you need to stop before OnStart is done, start an If you need to stop before OnStart is done, simply call ServiceBase.Stop.| async. thread, ie. using a Timer, call ServiceController.Stop from there. 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 -- Show quoteHope 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 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 > > 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. -- Show quoteHope 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 http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.stop.aspxnews:%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. | > | > 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 | > | > | | Dave,
| Due to time I would consider using ServiceBase.RequestAdditionalTime instead of doing | 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 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 There is also ServiceBase.Stop & ServiceBase.ExitCode| controller class? http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.stop.aspx http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.exitcode.aspx -- Show quoteHope 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? | | 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? > | > | > > |
|||||||||||||||||||||||