473,473 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Terminating a service

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
20 1386

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
Cor
Hi OHT,

Busy?

:-))

Cor
Nov 20 '05 #3
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
* "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
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
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
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
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
* "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
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
Cor
I did only want to say

I wrote see.
Nov 20 '05 #12
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
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
* "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

<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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: T Conti | last post by:
Hello: I created a basic windows service that polls a db and sends emails to our subscribers. In the onstart method I create a new thread and send it on its way to do work. It should never end...
9
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop() methods executed but I dont see the service...
23
by: Adam Clauss | last post by:
I have a C# Windows Service running as the NetworkService account because it needs to access a network share. As part of the service's initialization, I want the service to terminate, if an...
5
by: GTS | last post by:
Hi All, I am spawning a process from a service. The spawned process hungs for various reasons, (corrupted data, deadlock). I am expecting the process has to complete the task with in the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.