Terminating a service

  • Thread starter Thread starter Lee Schipper
  • Start date Start date
L

Lee Schipper

If a service needs to shut itself down, say due to a fatal error detected in
the program, what is the cleanest way to do that? Can I simply use the
Visual Basic End statement?

I only have a single service running in my process.

Thanks!
 
OHM wrote: <snip>you should at least try and dispose of any relevant objects
first<snip>

Yep. I would try to do all the same stuff that I do in my OnStop().
However the abruptness of the End bugged me and I wasn't sure if the system
would want to be formally notified that one of its services was going away.

Lee
 
* "Lee Schipper said:
Yep. I would try to do all the same stuff that I do in my OnStop().
However the abruptness of the End bugged me and I wasn't sure if the system
would want to be formally notified that one of its services was going away.

I wouldn't use 'End' at all.
 
Lee,
However the abruptness of the End bugged me and I wasn't sure if the system
would want to be formally notified that one of its services was going
away.
I'm sure it will! ;-)

I don't have a good answer for you, as I would not expect a Windows Service
to want to commit "suicide", in any circumstances! I would expect it to have
good error handling and log all errors, and maybe put itself into a stop
like state.

However if I had a real need for Windows Service "Suicide" than I would
consider creating a System.ServiceProcess.ServiceController to inform the
service it needs to shut down now.

Hope this helps
Jay
 
Hi Cor,
I've been very busy actually, since I started my new job, I
dont have hardley any time for the newsgroups, I spend all day writing
software in C#,VB and some in J#. Its very tough as C# is not something I
had much to do with but it's getting easier though.

How's things with you ?

Regards - OHM
 
Hi OHM,

Good to hear, J#, and VB or VB.net?
But keep it with the newsgroup, did want only to see that I did found it
nice to see you, becomes more and more quiet, only serious people it seems.

Very few fun, that to Herfried was a good one, "What would you do?"

But I have stopped prickle him, had done it enough, so now I do not do it
anymore by any situation I have a change. Sometimes I let him go. But not to
often you know, you cannot let those youngster do what they want.

:-))

Cor
 
Yes, in fact there is, however, the community is small at the moment.


Regards - OHM
 
Herfried wrote: <snip>I wouldn't use 'End' at all.<snip>

I agree wholeheartedly with the sentiment, just couldn't find any good
alternative.

Jay wrote: <snip>I would not expect a Windows Service to want to commit
"suicide", in any circumstances! I would expect it to have good error
handling and log all errors, and maybe put itself into a stop like
state.<snip>

I trap and *try* to recover from any error, but wanted to leave room for the
possibility that something might happen that I could not recover from.
Basically I am trying to resolve the "when all else fails" scenario. I had
envisioned that I would have the service removed from memory and have it
appear in the Service Control Manager as stopped.

I have had services spew out thousands of redundant errors into my log.
That irritates the heck out of me and I didn't want my service to be one of
"those" services -- mine should know when it was best to just throw in the
towel.

I did think about creating a ServiceController and sending a stop to myself,
but not knowing the exact mechanism of the stop request I got worried about
possible complications, and figured it was not worth the risk or trouble.
After all this would be code that *should* never get run anyway.

Besides, what if ServiceController.Stop() throws an exception...then what?
<GRIN>

Thanks for your feedback.

Lee
 
Lee,
Which was the point of my second suggestion.

Leave the Service running, but have it go to a "sleep mode". By "sleep mode"
I mean if its waiting for an Async event, don't wait for the event. If it
has a second thread, kill the second thread. If its using a Timer, stop the
Timer. If its using Remoting, stop Remoting. If its using TCPListener, stop
listening. What ever it is using to decide it has work to do, simple don't
do that! Let all threads (explicit or ThreadPool) exit without a request to
come back for more. Yes the service itself would still be "running" however
it would not be doing anything, it would effectively be waiting for commands
from the SCM.

Because as you suggested, I'm really not sure what will happen if you used
End, nor do I see a "polite" way of tell the SCM, hey I had a problem please
terminate me.

This would effectively mean that a person would need to stop & start the
service again (or do a restart), which can be a good thing, as the person
could diagnose any problems!

Or in the simplest sense: set a global variable to true, in all of your
routines if the global variable is true, exit routine!

Hope this helps
Jay
 
<snip>Which was the point of my second suggestion.<snip>

I didn't say it, but I was planning to go with the stop-like state you
suggested. Thanks!

Lee
 
Microsoft want it to grow to leverage the huge army of Java programmers over
to .NET. But I suspect that that is the main reason and once into .NET,
then convert them to C# which is not a million miles away from J# as far as
syntax goes.

OHM
 
Hi OHM,

And you think than to VB.net which is not a million miles away from C# as
far as
syntax goes?

:-))

Cor
 
Cor,

* "Cor said:
And you think than to VB.net which is not a million miles away from C# as
far as
syntax goes?

Migration path:

Java -> J# -> C# -> VB.NET

;-)))
 
Back
Top