473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 1384

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.