Thanks Laura and Willy. Something just seemed wrong with having this.Stop()
in the OnStart event procedure... Now I know...
"Laura T." <LT_stop@yahoo.com> wrote in message
news:eZI7K%23MBIHA.324@TK2MSFTNGP04.phx.gbl...
>
> "Smithers" <A@B.com> ha scritto nel messaggio
> news:%23aR1gNKBIHA.3716@TK2MSFTNGP03.phx.gbl...
>> I'm writing a new Windows Service. During the OnStart event procedure I
>> initialize a few things. If initialization fails, then I do not want for
>> the service to start.
>>
>> What I currently have is something like this:
>> if (All initialization tasks succeeded)
>> {
>> // log the fact that initialization succeded
>> // do some other stuff
>> }
>> else
>> {
>> // log the fact that initialization failed
>> this.Stop();
>> }
>>
>>
>> I'm not sure about my use of "this.Stop()" above. Does it even make sense
>> to put that in the OnStart event procedure...? has the service actually
>> started at this point? Is there a better way to accomplish the objective
>> of "not starting" the service when initialization tasks fail during the
>> OnStart event procedure?
>>
> The SCM asks you to start the service process, by calling the OnStart
> routine.
> Before calling your OnStart(), the ServiceBase sets the service status to
> pending so you don't have to.
> If you fail to start/initialize, of course first you must you must cleanup
> any resources you have, and then throw an exception from the OnStart().
> The exception is caught by the ServiceBase object that then sets the
> service status as stopped and writes the exception string to the event log
> for diagnostics, so it's a good idea to write detailed reason for the
> failure in the exception.
> Without exception, like a normal return;, would mark the process as
> started and SCM would list it as started, even that it is not.
>
> Using Stop(), would first override the SCM status to not started, but then
> if you do a clean exit from the OnStart(), the ServiceBase would still
> mark the service as running.
>
>
>
http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.aspx >
http://msdn2.microsoft.com/en-us/library/ms687414.aspx >
>
>
>> Thanks!
>>
>