Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

System.Threading.Timer and Window Service

$
0
0
Hi All,

    I have done some reading and have come to the conclusion that the System.Threading.Timer is best for a Windows Service scenario.

    In essnece, my situation is as follows:

    Upon starting my service (OnStart event), I open and read in an XML file and build my objects accordingly.  In essence, the objects contain information pertaining to certain Windows Services which I monitor (Ex.: SQLBrowser, SQLWriter, etc.).  I then instantiate a System.Threading.Timer object passing in a callback method, a state object (my list of services to monitor), a due time, and a period value.  I instantiate multiple timers, one for each group/set of services to monitor as each set may be on a different machine.  Each set has its own due time (delay) and period values.  My questions are the following:

     1.  To help with synchronization issues, I decorate the callback method with the [MethodImplOptions.Synchronized] attribute.  Is this the best approach?  I am aware that this locks the entire method.  This is where I use the state object (list of services to monitor) and determine whether to Start or Stop the services.  I am hoping this is a better approach to pausing/disabling the timer, doing my work and then re-enabling the timer.  I need a safe way to disable the timer or prevent the timer from re-entering the callback method while work is being done which may take a long time.

    2.  Is it best to call the Dispose method of my timer object in the OnStop event?  Keep in mind that I am instancing multiple timers and would like to stop all of them before stopping my service.

    3.  Is there a better approach/sample/example of a WatchDog type service which monitors other services and either starts or stops them on a timed/interval basis?


    Best Regards,

        Giovanni

Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>