By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,952 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,952 IT Pros & Developers. It's quick & easy.

Terminating a service

P: n/a
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!
Nov 20 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a

you should at least try and dispose of any relevant objects first.

OHM

Lee Schipper wrote:
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!


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #2

P: n/a
Cor
Hi OHT,

Busy?

:-))

Cor
Nov 20 '05 #3

P: n/a
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
Nov 20 '05 #4

P: n/a
* "Lee Schipper" <la*@nospamlabapps.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
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

"Lee Schipper" <la*@nospamlabapps.com> wrote in message
news:f7****************@twister.socal.rr.com... 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

Nov 20 '05 #6

P: n/a
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

Cor wrote:
Hi OHT,

Busy?

:-))

Cor


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #7

P: n/a
What would you do then Herfried ?

Regards - OHM

Herfried K. Wagner [MVP] wrote:
* "Lee Schipper" <la*@nospamlabapps.com> scripsit:
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.


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #8

P: n/a
Cor
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
Nov 20 '05 #9

P: n/a
* "One Handed Man" <O_************************@BTInternet.com> scripsit:
and some in J#.


Is there really someone using J# (taking on my glasses)?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #10

P: n/a
Yes, in fact there is, however, the community is small at the moment.
Regards - OHM
Herfried K. Wagner [MVP] wrote:
* "One Handed Man" <O_************************@BTInternet.com>
scripsit:
and some in J#.


Is there really someone using J# (taking on my glasses)?


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #11

P: n/a
Cor
I did only want to say

I wrote see.
Nov 20 '05 #12

P: n/a
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
Nov 20 '05 #13

P: n/a
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

"Lee Schipper" <la*@nospamlabapps.com> wrote in message
news:kW****************@twister.socal.rr.com...
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

Nov 20 '05 #14

P: n/a
* "One Handed Man" <O_************************@BTInternet.com> scripsit:
Yes, in fact there is, however, the community is small at the moment.


Do you think it will grow?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #15

P: n/a

<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


Nov 20 '05 #16

P: n/a
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
Herfried K. Wagner [MVP] wrote:
* "One Handed Man" <O_************************@BTInternet.com>
scripsit:
Yes, in fact there is, however, the community is small at the moment.


Do you think it will grow?


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #17

P: n/a
Cor
Hi OHM,

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

:-))

Cor
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#

Nov 20 '05 #18

P: n/a
Cor,

* "Cor" <no*@non.com> scripsit:
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

;-)))

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #19

P: n/a
I dont think so, more like

Java -> J# -> C# -> Java

;-)

OHM
Herfried K. Wagner [MVP] wrote:
Cor,

* "Cor" <no*@non.com> scripsit:
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

;-)))


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #20

P: n/a
In reading through this thread, I got lots of information. The problem I'm
having is that my service connects to other machines and occasionally they
get a 'General Network Error' which I don't know why is happening. I trap
the error but what I really want to do is stop the service (because I don't
know what state it is in) and restart it.

Anybody got any suggestions?

TIA - Jeffrey.

"One Handed Man" <O_************************@BTInternet.com> wrote in
message news:bv**********@sparta.btinternet.com...

you should at least try and dispose of any relevant objects first.

OHM

Lee Schipper wrote:
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!


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com

Nov 20 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.