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

Forcing a Windows service to shut itself down

P: n/a
Hi,

I wrote a Windows service for a client a few months ago, and the client has
now asked me to modify it so that it shuts itself down under certain
circumstances e.g. a catastrophic failure of the network, SQL Server
connectivity problems etc.

I've tried to explain that this probably isn't a very good idea nor is it
even necessary because the service has sufficient error and exception
handling code to take account of such situations and take remedial action
accordingly, but my client is insistent, citing the fact that "lots of
Microsoft services shut themselves down sometimes..."

So I'm looking for some advice, please.

Firstly, do you agree with me that Windows services shutting themselves down
is bad practice? I've searched on Google to see if I can find anything to
back me up, but have drawn a bit of a blank so far.

Secondly, is it actually possible for a Windows service written in C# (v1.1)
to shut itself down?

Any assistance gratefully received.

Mark
Dec 29 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
It all depends on the business situation. Those services that are started and
stopped on demand (such as MSIEXEC) have special purposes, different from
what this situation sounds like. On the face of it, you are right and the
client is wrong. Unfortunately, this goes against the marketing philosophy
that the client is always right, doesn't it?

The only way I know to have a service deliberately stop itself is the brute
force method - have it throw an unhandled exception.

--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Mark Rae" wrote:
Hi,

I wrote a Windows service for a client a few months ago, and the client has
now asked me to modify it so that it shuts itself down under certain
circumstances e.g. a catastrophic failure of the network, SQL Server
connectivity problems etc.

I've tried to explain that this probably isn't a very good idea nor is it
even necessary because the service has sufficient error and exception
handling code to take account of such situations and take remedial action
accordingly, but my client is insistent, citing the fact that "lots of
Microsoft services shut themselves down sometimes..."

So I'm looking for some advice, please.

Firstly, do you agree with me that Windows services shutting themselves down
is bad practice? I've searched on Google to see if I can find anything to
back me up, but have drawn a bit of a blank so far.

Secondly, is it actually possible for a Windows service written in C# (v1.1)
to shut itself down?

Any assistance gratefully received.

Mark

Dec 29 '05 #2

P: n/a
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:F0**********************************@microsof t.com...
It all depends on the business situation. Those services that are started
and
stopped on demand (such as MSIEXEC) have special purposes,
That's what I thought...
different from what this situation sounds like.
Quite right - this is just a bog-standard data processing service - every
five minutes it sniffs a particular directory on a remote VMS FTP server in
Canada and processes any files it finds up there before deleting them.
On the face of it, you are right and the client is wrong.
Which is never a happy position for a jobbing contractor to find himself
in... :-)
Unfortunately, this goes against the marketing philosophy
that the client is always right, doesn't it?
Oh indeed. Unless it is really bad for my reputation, the client always gets
what he wants, and I simply smile sweetly and raise the invoice... ;-)
The only way I know to have a service deliberately stop itself is the
brute
force method - have it throw an unhandled exception.


Ouch... I *really* don't fancy that very much...
Dec 29 '05 #3

P: n/a
couldnt you just instead of "stopping it" instead put the thread into
an extended sleep? I have done this on occassion for a service which
demands a sql server, and if there is a connectivity error, I set it to
sleep for 5 minutes, in the hopes that the server is just rebooting for
maintnance.

Dec 29 '05 #4

P: n/a
"mwolf" <mw***@realmagnet.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
couldnt you just instead of "stopping it" instead put the thread into
an extended sleep? I have done this on occassion for a service which
demands a sql server, and if there is a connectivity error, I set it to
sleep for 5 minutes, in the hopes that the server is just rebooting for
maintnance.


See my OP - I already have this sort of exception handling built in, but the
client has "specifically" asked for it to shut itself down...
Dec 29 '05 #5

P: n/a
hmmm if its runing as an admin, I think you should be able to do a
process start and run

net stop servicename

probably not the most elegant way, but think it would work, but havent
tried it

Dec 29 '05 #6

P: n/a
"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> wrote in
news:uc**************@TK2MSFTNGP14.phx.gbl:
Secondly, is it actually possible for a Windows service written in C#
(v1.1) to shut itself down?


I'm not going to argue the merits of this, but if you want to do it, you
can use the ServiceController class:

ServiceController sc = new ServiceController("MyServiceName");
sc.Stop();

-mdb
Dec 29 '05 #7

P: n/a
"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
ServiceController sc = new ServiceController("MyServiceName");
sc.Stop();


That sounds promising - I'll give it a try - thanks.
Dec 29 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.