As an exercise in both writing Windows Services and communicating with them, I've decided I want to try and write a service to monitor hardware performance on my machine, record it and report on it daily. I want to be able to query the service remotely and from researching this a little, I think I could embed a ServiceHost in there.
How does a service actually run? The examples 开发者_开发知识库I've found have all been, OnStart..OnStop, ServiceHost.Run( ) and that's it. Does a process run indefinitely, do I have to write a loop somewhere that constantly checks things?
Also, is it realistic to monitor windows machines using WMI through a service to report on disk space, IO and memory usage? From what I've read they're not the fastest and as I see it, my service would have a polling interval and check certain statistics every couple of minutes or so. Would this adversely affect a machine?
Have a look at this for an example (and explanation) of how to write a service very similar to what you're doing (ie repeatedly invoking some functionality).
To answer your question, you are responsible for doing something to make sure your code runs periodically. You can do this by registering a callback on a timer, or starting a thread that runs in a loop until the OnStop
method is called.
As for monitoring the machine through WMI -- the volume of statistics you're collecting at an interval of every few minutes shouldn't cause a problem. I've seen cases where a lot of data was collected, relatively often, without significant impact.
I like to implement services where the Start/Stop/Shutdown starts and stops a timer, where the interval is set in the config.
When the the timer event fires, I usually stop the timer, process the tasks and re-start the timer (the start/stop shut-down sets a flag which is checked before restarting) This caters for long running tasks (ie tasks that may run over the interval time if its set really short)
If you are checking every couple of minutesthe speed of WMI is probably irrelevant, but in any case, investigate various ways of reporting on those criteria and look for the most efficient ways.
Another great thing to do is to adjust the Program.cs so the application can take parameters and install/uninstall itself and to run the tasks once by just running the exe (ie not as a service) This helps debugging the service. see: Installing Windows Service programmatically
Windows services receive start and stop commands (control requests) via the Service Control Manager
(SCM) service.
This "super service" starts early in the Windows startup sequence, and is responsible for bringing all the other services up.
The service functions provide an interface for the following tasks performed by the SCM:
- Maintaining the database of installed services.
- Starting services and driver services either upon system startup or upon demand.
- Enumerating installed services and driver services.
- Maintaining status information for running services and driver services.
- Transmitting control requests to running services.
- Locking and unlocking the service database.
See Starting Services on Demand for pointers to available SCM functions:
The user can start a service with the Services control panel utility. The user can specify arguments for the service in the Start parameters field. A service control program can start a service and specify its arguments with the
StartService
function.
Regarding the load generated by monitoring functions, you'll have to play with the polling interval and decide what overhead is acceptable. Usually, intervals of 2 to 5 minutes present no problem.
精彩评论