Hi,
I'm working for Windows Service which need to do short basic task only
once per day. I think to start it using Scheduled Tasks, and propably
Scheduled Task will run just bat-file to start the service - or better
suggestions?
Then when service has done the basic task, it should shutdown by itself
(to save computer resources) - until Scheduled Task starts it again
(next day).
Question: How to make C# 2005 Windows Service to shultdown itself?
I don't entirely agree with the other posters who have tried to steer
you away from a Windows service to solve your problem. Without knowing
exactly what procedures will be executed during your scheduled task
(i.e. it's purpose). Programmaticly adding and removing a service can
be achieved through .NET (see link below), but you'll find many
software vendors continuing to implement their own scheduler inside a
Windows Service that identifies itself to the user. (e.g. Apple's
iTunes/QuickTime update service). If your service exist to facilitate
another application (as would be the case with an update service) it
would probably make more sense to implement this as a Windows Service.
You other applications can easily communicate with that service; stop
or re-start it if need be.
Having said that, you would not have the Windows Service in a stopped
or disabled state. This would be an action the user would be free to
take if they didn't want the service operating for whatever reason and
they would accomplish that task through the Services settings in
Administrator Tools, Control Panel. Inside your service you would have
a TimerThread that executes every x milliseconds. Effectively the
service always runs in the background as its own scheduler. The
advantages this approach has: It's easier for the UI driven app the
service is facilitating to monitor the health of this service (e.g. on
launch, with a few lines of code you can determine if the service is
running and if not re-start) also many Installer applications support
installation and removal of Windows Services to get an application
installed into the scheduler will be a bit more code intensive, also
it's relatively easy to assign (programmaticly) and to reassign
programmability or manually for the user the Windows system account
that the service will be running under..
Reference:
http://www.codeproject.com/KB/cs/tsnewlib.aspx