Forcing a Windows service to shut itself down

M

Mark Rae

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
 
G

Guest

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
 
M

Mark Rae

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...
 
M

mwolf

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.
 
M

Mark Rae

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...
 
M

mwolf

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
 
M

Michael Bray

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top