System.Timers.Timer Exception Number must be either non-negative or -1. Parameter name: dueTime

i got this exception while calling Timer.Start()

Number must be either non-negative or -1.
Parameter name: dueTime

can any one tell me the reason.
timer interval is set to 180000.



Answer this question

System.Timers.Timer Exception Number must be either non-negative or -1. Parameter name: dueTime

  • RICH525234

    Check the value again, the exception means you've put a positive number in that is too large, and so gets turned into a negative number when converted to an int.



  • rSchild

    i dont get any error when calling timer.Interval = Math.Pow(2, 31) + 1.

    what timer are u using, is it system.timers.timer. for system.timers.timer interval is double and i dont think it can break with such small number. i m using .net 1.1



  • tamri

    I fired up on old PC with VS2003 to test your program. Same exception (on t.Enabled = true;), slightly different message: "Number must be either non-negative or -1."

    I have no idea why you wouldn't get an exception. However, it reproduces well with your original problem, somehow the Interval property gets set to an out-of-range value...



  • hwiz

    In VS2005, when I execute this:
    Timer.Interval = 2^31 + 1
    I get an ArgumentOutOfRange exception: "Number must be either non-negative and less than or equal to Int32.MaxValue or -1". That's pretty close to your exception. Your expression value is not close to triggering that, unless timeToWaitInWin = 3*60*1000...

    Less likely: check if this only happens when the PC has been last booted 25 days ago (2^31 msec).


  • TheTheTheTheTheThe

    just tried this, got no error

    static void Main(string[] args)

    {

    Timer t = new Timer();

    t.Elapsed += new ElapsedEventHandler(TimerElapsed);

    t.Interval = Math.Pow(2, 31) + 1;

    t.Enabled = true;

    Console.ReadLine();

    }

    public static void TimerElapsed(object sender, ElapsedEventArgs Args)

    {

    Console.WriteLine("Elapsed");

    }



  • moemen.ahmed

    I do, using your exact C# code in VS2005. It bombs on t.Enabled = true; Sorry, I don't have VS2003...



  • KervyChoa

    tried the same code on couple of other pcs, got no error there as well, may be it throws exception on some type of pcs and dont throw any on others.

  • Martimus

    i m using timer.Interval = timetoWaitinMin * 60 * 1000 for assigning to timer,

    where timertoWaitInwin = 3.

    this property in double so i dont think i can assign such a large number to it that it can turn into -ve, also it should give exception when assigning and not at timer.start().

    i m using it in a service, which was running successfully for hours resuming after every 3 minutes and for once it stopped just because this call failed and timer didnt elapsed.



  • nglow

    OK - that's the problem then. It's a shame it doesn't deal with that better.



  • Anjan Ghose

    Yes, System.Timers.Timer, but on .NET 2.0. It probably only bombs if the timer is enabled. If it isn't, it should bomb on Enabled = True. Internally, it converts Interval to an integer with:
    int num1 = (int)Math.Ceiling(this.Interval);




  • System.Timers.Timer Exception Number must be either non-negative or -1. Parameter name: dueTime