Windows Service with System.Timers.Timer 
Author Message
 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/



Wed, 04 Aug 2004 05:12:33 GMT  
 Windows Service with System.Timers.Timer
Why don't you start your procedure that checks the database on its own
thread and then use the Sleep(milliseconds) method to put the thread to
sleep for the 5 seconds or whatever between checks of the database.  Sounds
like this would be cleaner.

HTH
Tom


Quote:
> 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.prairiefyre.com



Fri, 06 Aug 2004 08:55:46 GMT  
 Windows Service with System.Timers.Timer
Thanks for the reply.

It seems that I need a timer no matter how I slice the problem.  Only two
timers are available, and one requires a windows input queue, which services
don't have.  This leaves me with System.Timers.Timer...

--
Robert Chapman, MCSD
Manager, Applications Development
prairieFyre Software Inc.
http://www.prairiefyre.com

Quote:
> Why don't you start your procedure that checks the database on its own
> thread and then use the Sleep(milliseconds) method to put the thread to
> sleep for the 5 seconds or whatever between checks of the database.
Sounds
> like this would be cleaner.

> HTH
> Tom



> > 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.prairiefyre.com



Fri, 06 Aug 2004 22:47:24 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. system.timers.timer bug found

2. System.Timers.Timer in VB .NET ignores errors

3. Server Timers vs. Windows Timers for RS232 polling

4. System.Timer on a Service

5. VB Timer Windows Service.

6. Timer in a Windows Service Not Ticking...(Help)

7. memory leak using system.windows.forms.timer

8. Benefits of the timer object, bompared to the timer control

9. better timer than Timer?

10. timer event within a timer event

11. Q: Timer problem (need simple count up timer)

12. Timer interval is depending on code in timer event

 

 
Powered by phpBB® Forum Software