
Windows Service with System.Timers.Timer
Let me first say that I don't have a lot of practical experience with
threading, I've read lots about and I get the concepts, but that's about it.
I need to create a service that polls a database every couple of seconds and
acts on a result set if need be. I don't care about any other program
communicating with the service, it just needs to up 24/7 and working. To
implement this polling I created an instance of System.Timers.Timer and I
added code to the Elapsed event handler.
I noticed that this timer is re-entrant, so if my interval is set to 1
second and the unit of work that needs to be performed in the elapsed event
takes 3 seconds, then the service will spin up 2 more threads and run the
elapsed event handler on each one. As you can imagine this is not how I
want things to run.
As a workaround I call the Stop method on the timer as the first statement
in my elapsed event handler, then I call Start as the last statement. For
obvious reasons this stops the timer from being re-entrant and assures that
only one thread is polling the database at a time.
Is this approach totally bogus, am I making this much harder than it needs
to be?
TIA
Cheers
--
Robert Chapman, MCSD
Manager, Applications Development
prairieFyre Software Inc.
http://www.*-*-*.com/