system.timers.timer for very large intervals

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I created system.timers.timer for very long interval from TimeSpan.MaxValue:

myTimer = new System.Timers.Timer(TimeSpan.MaxValue.TotalMilliseconds)

After calling Enable = true I have a ArgumentOutOfRangeException exception.
I found out that timer internally uses System.Threading.Timer object and uses
constructor with "int" for dueTime. I suppose it explicitly converts double
to int without checking for overflow and System.Threading.Timer is created
with negative number for dueTime which throws an exception.
Is it a bug ?
 
Peter said:
I created system.timers.timer for very long interval from TimeSpan.MaxValue:

myTimer = new System.Timers.Timer(TimeSpan.MaxValue.TotalMilliseconds)

After calling Enable = true I have a ArgumentOutOfRangeException exception.
I found out that timer internally uses System.Threading.Timer object and uses
constructor with "int" for dueTime. I suppose it explicitly converts double
to int without checking for overflow and System.Threading.Timer is created
with negative number for dueTime which throws an exception.
Is it a bug ?

No. The argument is obviously out of range, so what? ;-)

bye
Rob
 
Considering the documentation doesnt mention any
ArgumentOutOfRangeExceptions Id say its a bug yes.
If you really need to wait for a very long time before doing something you
should probably use a thread instead, which could contain an DateTime with
the time it should call a function.
 
Peter said:
Out of range ??? There is no range in documentation and no
ArgumentOutOfRangeException exception expected (in documentation). So I
consider it as bug.

myTimer = new System.Timers.Timer(TimeSpan.MaxValue.TotalMilliseconds)

TimeSpan.MaxValue is equivalent to 9,223,372,036,854,775,807,
And this IS out of every range I can think about.

bye
Rob
 
System.Timers.Timer takes a Double. I set it to Double.MaxValue with no
exceptions.
What version of .NET framework are you using?
 
Peter Maco said:
I created system.timers.timer for very long interval from
TimeSpan.MaxValue:

myTimer = new System.Timers.Timer(TimeSpan.MaxValue.TotalMilliseconds)

After calling Enable = true I have a ArgumentOutOfRangeException
exception.
I found out that timer internally uses System.Threading.Timer object and
uses
constructor with "int" for dueTime. I suppose it explicitly converts
double
to int without checking for overflow and System.Threading.Timer is created
with negative number for dueTime which throws an exception.
Is it a bug ?


I would say this is a "documentation" bug.
The System.Timers.Timer takes a double as argument, but the value must be 0
=> interval <= Int32.MaxValue, or -1.
The same goes for System.Threading.Timer which takes an int or a long as
interval, but again the value of the interval is restricted to Int32.MaxVal.
This is because the underlying OS WaitableTimer object uses a long (signed
32 bit in C) for the period of the timer.


Willy.
 
Back
Top