Search code examples
c#.netwindowswcfservice

Creating a service for monitoring


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?


Solution

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