I have a once Timer that calls a function every 15 seconds, it works for the first 5 times, but then does not elapse for the 6th, even though it is created, any ideas?
The processing time could take longer than 15 seconds, if so Thread A could be processing data while Thread B sends out a new request for data. Thread B cannot start until the processing of Thread A has finished.
const int DATAREFRESH = 15000;
void RequestUpdate()
{
// Some data processing goes here
Console.WriteLine("Update");
// Set the timer
Timer t = new Timer(new TimerCallback(TimeOutCallback), null, DATAREFRESH, Timeout.Infinite);
}
private void TimeOutCallback(object state)
{
RequestUpdate();
}
In the Output window, “Update” is displayed 5 times, then after then, nothing. It hasnt frozen, I see that ~8 threads terminate after the last “Update”.
How can I get this to work infinitely?
It seems very weird to instantiate a new timer from within the callback. A more realistic example would be one in which you are performing the work inside the timer callback. Also you seem to be incorrectly calling the
Timerconstructor. If you want the callback to execute at regular intervals (15 seconds in your case) you could use the following:Also notice how I have inverted the arguments passed to the timer so that it executes the callback every 15 seconds.