Timer Elapsed Firing Twice

B

Brian P

I have a service that is driven by a timer that fires every 5 seconds.
For the most part, it works fine. But every once in a while the timer
fires twice. In the log I can see that when it fires twice, there are
two threads running. This causes problems so I need to find a way to
fix it.

So, my question is, do I need to use a Monitor to lock in my
timer_elapsed event handler?

Thanks for any advice...

Brian
 
G

Greg

What sort of timer did you use? I had problems with the timer present
in the toolbox due to changes in controls on the form getting in the
way of the timer's elapsed method. I'm wondering if the timer is firing
twice because the first time it should have been firing something else
was happening.

Switching to a System.Timers.Timer solved the issues that I had.

Greg.
 
B

Brian P

I am using a System.Timers.Timer.

It is a windows service, so it isn't form based.

Thanks for the thought, tho.

-B
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Maybe what happen is that in those moments the timer handler takes more than
5 seconds from the moment the timer elapsed for the last time.
a lock may be of help here.


cheers
 
B

Brian P

From my log, it seems that the two timer events occur concurrently.
It's almost as if the timer firest two elapsed events at the same time.
Because the work occurs in two different threads. (One for each event?)

I did add a lock and we'll see what that does..

--Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top