Re: How should a Windows Service stop itself?

Tech-Archive recommends: Speed Up your PC by fixing your registry



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_MVP@xxxxxxxxxxxxx> wrote in
message news:uRMp9CvuGHA.1772@xxxxxxxxxxxxxxxxxxxxxxx
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" <rhuseby2@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:Xns98189A468B484runehusebynewschello@xxxxxxxxxxxxxx
| "Dave" <none@xxxxxxxxxxx> wrote in
| news:ehu4X1duGHA.4752@xxxxxxxxxxxxxxxxxxxx:
|
| > 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




.



Relevant Pages

  • Re: How should a Windows Service stop itself?
    ... service or do the other things that the Recovery option allows. ... use the service controller class to stop the service then Windows doesn't ... the OnStart event so that the OnStart event can complete within the 30 ...
    (microsoft.public.dotnet.framework)
  • Re: Service unable to start
    ... When a service is started (by Windows), Windows invokes the OnStart method within the Service. ... Your code does not return from OnStart because OnStart calls RunIt which is your "main" processing loop. ... Private Sub tmrGo_Tick(... ... Private Sub OnStop(... ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Probem in launching new process from a c# .net windows service.
    ... the problem is that you spend too much time in your OnStart method. ... This method is meant to initialize service after which it spawns another ... open the windows Task Manager to see how many process will be ...
    (microsoft.public.dotnet.framework)
  • Re: Probem in launching new process from a c# .net windows service
    ... I have put my test projects here, ... the problem is that you spend too much time in your OnStart method. ... open the windows Task Manager to see how many process will be ... >>> message DLL files to display messages from a remote computer. ...
    (microsoft.public.dotnet.framework)
  • Re: windows service restart
    ... the service process exe still running? ... I suspect all this may have something to do with your OnStart. ... Phil Wilson [MVP Windows Installer] ... - set 'restart the service' for the first, second and subsequent failures, ...
    (microsoft.public.dotnet.general)