1

I am in C# .NET 3.5. It seems, that by registering more than 10 System.Threading.Timers from same thread causes the first ones to die off somehow... Can't believe it though. If that was true, I'd be more than shocked.

Here is the line, where I start a timer, each time a new message arrives:

System.Threading.Timer tmr = new System.Threading.Timer(WaitAckElapsedTmrHandler, msgId, Constants.TIME_TO_WAIT_FOR_ACK, Timeout.Infinite);

What else could be my problem ? I believe have properly locked all thread shared resources and even carefully log all in and outs to/from locked zones -> so I can't see any deadlocks.

How can I detect a deadlock in such a case as this ?

Sold Out
  • 1,321
  • 14
  • 34
  • 3
    That is not the case at all. Creating multiple `Timer` objects doesn't destroy earlier ones. – Servy Jun 04 '15 at 13:55

1 Answers1

5

System.Threading.Timer can be garbage collected if you do not keep a reference to it alive. After about 10 messages it appears that the GC kicks in and kills off your timers.

Switch to a different timer that does not get garbage collected like System.Timers.Timer or keep some kind of reference to the timer instead of letting the variable go out of scope and the program should work.

See this SO question for a more in-depth explanation about why a System.Timers.Timer will survive GC but System.Threading.Timer will not.

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431