System.Threading.Timer fires too quickly!

  • Thread starter Thread starter Kenny
  • Start date Start date
K

Kenny

I am running a windows service that takes actions based on a couple
System.Threading.Timers. The intervals are usually short... based on
the time of day, anywhere between 1 and 5 minutes. Recently however,
the event that was firing based on the timer started firing rapidly; it
fired about 9000 times in a minute and a half. After the minute and a
half was up, it returned to normal behavior!

.... just wondering if anyone has experienced this strange behavior and
if they've found a solution. There was another posting on this a few
months ago, but there were no responses
(http://groups-beta.google.com/group...read/thread/c9d322fddea7c89d/f15803fd38be2abe)

Much thanks for any help!
 
Kenny said:
I am running a windows service that takes actions based on a couple
System.Threading.Timers. The intervals are usually short... based on
the time of day, anywhere between 1 and 5 minutes. Recently however,
the event that was firing based on the timer started firing rapidly; it
fired about 9000 times in a minute and a half. After the minute and a
half was up, it returned to normal behavior!

... just wondering if anyone has experienced this strange behavior and
if they've found a solution. There was another posting on this a few
months ago, but there were no responses
(http://groups-beta.google.com/group...read/thread/c9d322fddea7c89d/f15803fd38be2abe)

Much thanks for any help!

"based on time of day", could it be that there are specific times
that are between the "usual" periods that end up with an interval
of 1/100 seconds? Say "time < 08:00" = 5 minutes,
"time > 08:00" = 1 minutes, then what about 08:00 itself?
How exactly are you calculating that interval?
 
I have a loop that does some work. In my config file, I specify a
value "peak hours". At the top of the loop, I check to see what time
it is. If I am within the peak hours, my interval is 1 minute,
otherwise it is 5 minutes. I set the timer with one of those values.
The body of the loop does some work, and at the bottom of the loop, I
wait for an AutoResetEvent that is set by the callback of the Timer.

I've debugged the code and it just doesn't seem possible for the
interval to have a value other than 1 or 5 because I'm using an if-else
to set the interval value. If it's within peak hours, set it to 1,
else 5.

Here is the main bit of the code...
public void StartMessages(object state)
{
int timeBetweenRequests;
Timer requestTimer;
AutoResetEvent[] events = new AutoResetEvent[2];

while( true )
{
timeBetweenRequests = GetRequestInterval();

for(int i = 0; i < m_Requestors.Count;)
{
RequestorData rd = (RequestorData)m_Requestors;

requestTimer = new Timer(new TimerCallback(this.SignalNextRequest),
m_SendNextRequest,
timeBetweenRequests, Timeout.Infinite);

try
{
if(rd.Type == RequestorType.DialUp)
{
DoDialUpRequest(rd);
}
else if(rd.Type == RequestorType.Network)
{
DoNetworkRequest(rd);
}
else
{
throw new V1ErrorException(ErrorInfo.InvalidRequestorType);
}
}
catch(V1ErrorException v1Err)
{
ExceptionManager.Publish(v1Err);
}

events[0] = m_SendNextRequest;
events[1] = m_EndRequestLoop;

int eventIndex = WaitHandle.WaitAny(events);
requestTimer.Dispose();

if(eventIndex != 0)
{
Debug.WriteLine("Signal received to kill request group. Ending
request loop.");
return;
}
}
}
}

private int GetRequestInterval()
{
DateTime now = new DateTime(DateTime.Now.Ticks);
int currentTime = now.Hour * 100 + now.Minute;

if( currentTime >=
Convert.ToInt32(MonitorConfigReader.MonitorSettings("peakHoursStart"),
CultureInfo.InvariantCulture) &&
currentTime <=
Convert.ToInt32(MonitorConfigReader.MonitorSettings("peakHoursEnd"),
CultureInfo.InvariantCulture) )
{
return
Convert.ToInt32(MonitorConfigReader.MonitorSettings("peakHoursRequestInterval"),
CultureInfo.InvariantCulture);
}
else
return
Convert.ToInt32(MonitorConfigReader.MonitorSettings("offPeakHoursRequestInterval"),
CultureInfo.InvariantCulture);
}
 
Kenny said:
I have a loop that does some work. In my config file, I specify a
value "peak hours". At the top of the loop, I check to see what time
it is. If I am within the peak hours, my interval is 1 minute,
otherwise it is 5 minutes. I set the timer with one of those values.
The body of the loop does some work, and at the bottom of the loop, I
wait for an AutoResetEvent that is set by the callback of the Timer.

I've been running into a similar situation. For me it has only been on
setting a very long dueTime > 8 days or so. Instead of waiting that
amount of time, it fires immediately.

I tried to narrow it down, to a specific dueTime, but it seemed that as
the system time progressed, the dueTime that started to demonstrate this
problem decreased. Currently, it seems that if I try to set a dueTime
that will expire after a fluctuating time on 2005-04-07 it demonstrates
this behavior. And the time seems to be getting slowly earlier...

I'm curious what will happen as that date gets closer. Will it fire
right away with a dueTime as low as 1 minute then? After that point in
time, what date will start giving this problem? It definitely seems
like some sort of bug to me, though what they could have done to
demonstrate this odd behavior is beyond me.
 
Back
Top