473,509 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forcing a Windows service to shut itself down

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

Similar topics

0
3383
by: Ross Bennett | last post by:
Ahoy, Folks! I've been looking all over for this, but I just can't seem to shake any documentation out of the MSDN or from Google. I've reviewed every .NET article on developing Windows...
4
5834
by: Claire | last post by:
Running XP pro, SP2. Visual studio .NET 2003. App written in C# I have written an application that hides itself when run and shows a notification icon in the system tray. If the main form is...
9
7231
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...
5
1315
by: Mark Rae | last post by:
Hi, I'm looking for advice concerning what to do if a Windows service does not find a "viable" working environment at startup. E.g. I have a Windows service which does the following: 1)...
6
8738
by: carbon_dragon | last post by:
Ok, so here is the problem. I'm working on a headless server program implemented as a .NET C# Console project. There is a UPS mounted to this server (though not a windows compliant UPS). I can only...
2
3640
by: Shawn Meyer | last post by:
Hello, I have a windows service. When the onstart fires, my app creates two threads. The first is a processing thread and the second is a watcher that makes sure the everything is ok with the...
4
1941
by: jez123456 | last post by:
Hi Experts I've written a c# windows service which runs another program at certain intervals. The other program may take upto 20 minutes to complete it's tasks. My question is what happens to...
0
999
by: Derek Hart | last post by:
I have a vb.net winforms application that I want to run every time the server starts up (it will be running on a server, and it is okay to have a user interface). I have had a ton of trouble...
0
2021
by: ArkJ | last post by:
Hello. I have a little problem. I created a little Service which uses SIP, all works rather well, but when I want to shut it down in the Services panel, it looks as if it's shut down, but in fact...
0
7344
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
7412
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
7069
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
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
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
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...
1
775
muto222
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.