Timer Callback and Overlap

  • Thread starter Thread starter Dinesh Eswaran
  • Start date Start date
D

Dinesh Eswaran

Hi,
I use the Timer from System.Threading to call the function 'myFunc'
every 1000 msecs. myFunc is executed from the .NET thread pool

// 'myFunc' is the call back function
myCallback = new TimerCallback(myFunc);
// Set up timer to call 'myFunc' every 1000 msec
myTimer = new Timer(myCallback, null, 1000, 1000);

What happens when there is an overlap? i.e., A call to 'myFunc' takes
more than 1000 msecs and meanwhile a second call arrives? In my test I
am finding that the first call just exits midway! Can this be true?

-- Dinesh
 
If it overlaps, it will start another one on another thread. I very much
doubt it will kill your original, but if you've been looking at this in the
debugger, it will look very odd indeed.

I would seriously look at why you need to schedule more that 1000msecs work
every 1000msecs. However, you could place a Monitor around you code. If you
can live with missing some ticks, then use TryEnter (and conditionally
continue), otherwise use Wait (I would be very very cautious of this,
though).

Nick Holmes.
 
Hi,

My past experience makes me believe that the function will not exit half
through, but it will get called again from another thread in the thread
pool. This is a very quick and effective way to starve the thread pool.
In the past I have written some management code around Timers to prevent
this from happening.

Regards,

- Bruce.
 
I disagree. Theoretically, they may exit due to exceptions. The handler is
re-entrant by definition, however if the code behaves in a stateful way (by
creating and manipulating stateful variables for example), this may cause
one of the objects in the re-entrant code to fail percolating the exception
thru each thread touching that variable. The net result of the uncaught
exception is to exit the call back.

I question whether or not a timer is needed to fire off every second in the
first place.
 
Thanks a lot for all your answers. I was not able to find the same
information in MSDN, so your answers were valuable.

From your answers I feel i am wrong about the thread exiting. I am not
maintaining state and that could be screwing up a lot of things.

-- Dinesh





Alvin Bruney said:
I disagree. Theoretically, they may exit due to exceptions. The handler is
re-entrant by definition, however if the code behaves in a stateful way (by
creating and manipulating stateful variables for example), this may cause
one of the objects in the re-entrant code to fail percolating the exception
thru each thread touching that variable. The net result of the uncaught
exception is to exit the call back.

I question whether or not a timer is needed to fire off every second in the
first place.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Jakob Christensen said:
Hey Dinesh,

Your calls will NOT exit the callback function midway. The calls to your
callback function will "overlap", but they will run on separate threads
(don't forget to protect your shared ressources with critical sections).
Notice that the threads are taken from the thread pool. If your callback
function takes to long to execute you may force the thread pool to use all
its threads for your timer. In that case new timer callbacks will be
queued until free threads are available.

Regards, Jakob.
 
Back
Top