[Mono-list] Timer.Change bug in mono 1.9.1?
Martin Blitz
mcblitz at verizon.net
Wed May 21 23:53:51 EDT 2008
I have a problem that I wrote using mono 1.2.5, and when I recompiled with
mono 1.9.1, it started to act badly. I traced it down to the Timer.Change
function.
The program uses timer for polling. I don't want to use the Timer "period"
field, since I want to wait until the polling function is done before
starting the timer again, so I use the Timer.Change method to reset the
"dueTime".
I Wrote a quick test program to test the Timer.Change function. It
basically creates 10 objects that each create a timer, wait 3 seconds for
the timer to trigger, then set the timer again. After only 7 iterations,
all the timers failed to trigger.
Main.cs:
// project created on 5/21/2008 at 5:49 PM
using System;
using System.Collections;
using System.Threading;
namespace TimerBug
{
class MainClass
{
private const int MaxTimers = 10;
public static void Main(string[] args)
{
ArrayList timers = new ArrayList();
for (int i = 0; i < MaxTimers; i++) timers.Add(new
TimerSample(i));
Thread.Sleep(Timeout.Infinite);
}
}
}
TimerSample.cs:
using System;
using System.Threading;
namespace TimerBug
{
public class TimerSample
{
private const Int32 PollPeriod = 3000;
private Timer timer;
private int id;
private int count = 0;
public TimerSample(int id)
{
this.id = id;
timer = new Timer(TimerHandler, null, PollPeriod,
Timeout.Infinite);
}
private void TimerHandler(object o)
{
Console.WriteLine("Handling timer " + id + "." + count);
count++;
timer.Change(PollPeriod, Timeout.Infinite);
}
}
}
Output:
Handling timer 0.0
Handling timer 3.0
Handling timer 5.0
Handling timer 2.0
Handling timer 7.0
Handling timer 4.0
Handling timer 9.0
Handling timer 6.0
Handling timer 8.0
Handling timer 1.0
Handling timer 2.1
Handling timer 3.1
Handling timer 5.1
Handling timer 7.1
Handling timer 4.1
Handling timer 9.1
Handling timer 6.1
Handling timer 8.1
Handling timer 1.1
Handling timer 2.2
Handling timer 3.2
Handling timer 5.2
Handling timer 7.2
Handling timer 4.2
Handling timer 9.2
Handling timer 6.2
Handling timer 8.2
Handling timer 1.2
Handling timer 4.3
Handling timer 9.3
Handling timer 6.3
Handling timer 8.3
Handling timer 1.3
Handling timer 4.4
Handling timer 9.4
Handling timer 6.4
Handling timer 8.4
Handling timer 1.4
Handling timer 4.5
Handling timer 9.5
Handling timer 6.5
Handling timer 8.5
Handling timer 1.5
Handling timer 4.6
Handling timer 9.6
Handling timer 6.6
Handling timer 8.6
Handling timer 1.6
Handling timer 4.7
Handling timer 9.7
Handling timer 6.7
Handling timer 8.7
Handling timer 1.7
Then the output stops.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080521/7f0da397/attachment-0001.html
More information about the Mono-list
mailing list