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

Initialising a Windows service

P: n/a
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) connects to a remote FTP site and looks for certain files which it
downloads to a fileserver if it finds them;

2) processes the files and writes their contents into various tables in
various databases on various servers;

3) generates a mail message containing various statistics to various
Exchange and POP3 mailboxes.

When it starts up, it calls a function which

1) attempts a logon to the remote FTP site;

2) tries to create (and then delete) an empty text file on the file server;

3) sends a test email to the network manager.

If any one of the above processes fails on startup, the service considers
that it doesn't have a viable working environment in which to run, so
disables the System.Timer object.

I've now been asked that this should actually stop the service altogether,
because the network has service monitoring software which would detect this
and send alerts etc.

Would you consider that this was a bit too drastic, and that the service
should continue to try to find a viable working environment, at least for a
specified number of times?

If not, what is the safest / cleanest way for a Windows service to shut
itself down?

Any asssistance gratefully received.

Mark
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I asked something similiar not too long ago, and the answer I got is in your
service OnStart method, if the environemnt isn't viable, throw an exception.
This is supposed to prevent the service from even starting... I haven't had
an opportunity to try it out yet, hopefully I can get back to that project
this week sometime...

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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) connects to a remote FTP site and looks for certain files which it
downloads to a fileserver if it finds them;

2) processes the files and writes their contents into various tables in
various databases on various servers;

3) generates a mail message containing various statistics to various
Exchange and POP3 mailboxes.

When it starts up, it calls a function which

1) attempts a logon to the remote FTP site;

2) tries to create (and then delete) an empty text file on the file
server;

3) sends a test email to the network manager.

If any one of the above processes fails on startup, the service considers
that it doesn't have a viable working environment in which to run, so
disables the System.Timer object.

I've now been asked that this should actually stop the service altogether,
because the network has service monitoring software which would detect
this and send alerts etc.

Would you consider that this was a bit too drastic, and that the service
should continue to try to find a viable working environment, at least for
a specified number of times?

If not, what is the safest / cleanest way for a Windows service to shut
itself down?

Any asssistance gratefully received.

Mark

Nov 17 '05 #2

P: n/a
"JSheble" <js************@logicor.com> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
I asked something similiar not too long ago, and the answer I got is in
your service OnStart method, if the environemnt isn't viable, throw an
exception. This is supposed to prevent the service from even starting... I
haven't had an opportunity to try it out yet, hopefully I can get back to
that project this week sometime...


I'll check it out - thanks for the reply.
Nov 17 '05 #3

P: n/a
Let me know how it goes...

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:Ot**************@TK2MSFTNGP10.phx.gbl...
"JSheble" <js************@logicor.com> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
I asked something similiar not too long ago, and the answer I got is in
your service OnStart method, if the environemnt isn't viable, throw an
exception. This is supposed to prevent the service from even starting...
I haven't had an opportunity to try it out yet, hopefully I can get back
to that project this week sometime...


I'll check it out - thanks for the reply.

Nov 17 '05 #4

P: n/a

"JSheble" <js************@logicor.com> skrev i en meddelelse
news:u$**************@tk2msftngp13.phx.gbl...
Let me know how it goes...

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:Ot**************@TK2MSFTNGP10.phx.gbl...
"JSheble" <js************@logicor.com> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
I asked something similiar not too long ago, and the answer I got is in
your service OnStart method, if the environemnt isn't viable, throw an
exception. This is supposed to prevent the service from even starting...
I haven't had an opportunity to try it out yet, hopefully I can get back
to that project this week sometime...


I'll check it out - thanks for the reply.


Absolutely throwing an exception will stop the service from starting. I've
seen this many times with services I've written... not that they were
supposed to thrown exceptions, but.
Nov 17 '05 #5

P: n/a
mdb
"JSheble" <js************@logicor.com> wrote in
news:OB**************@TK2MSFTNGP10.phx.gbl:
I asked something similiar not too long ago, and the answer I got is
in your service OnStart method, if the environemnt isn't viable, throw
an exception. This is supposed to prevent the service from even
starting... I haven't had an opportunity to try it out yet, hopefully
I can get back to that project this week sometime...


Unless you (a) can determine the viability of the environment fairly
quickly, and (b) want windows to report that the service failed to start, I
wouldn't use an exception in OnStart(...) because if the OnStart(...) takes
too long then Windows may report that the service didn't start in the
expected amount of time (its like 30 seconds or something I'm not sure.)
Usually, this is not desirable.

Instead, you should have the OnStart(...) start up a new thread that tries
to establish the environment. If it is successful, then continue
processing as you would otherwise. If it is NOT successful, and you want
the service to stop (without an error), then you can use the
ServiceController class to issue a stop command to yourself.
--
-mdb
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.