Timer Overflow error

I am using Visual studio 2005 express edition. I have written a VB.NET application which uses a couple of System.Windows.Forms.Timer (s) that trigger events every second.  Everything runs just fine.  However, if I leave the application running overnight, I return to an Overflow error from the timer.  I guess it has some internal counter that is getting to a too large of a value   The error:

 

************* Exception Text **************
System.OverflowException: Arithmetic operation resulted in an overflow.
   at Application1..Form1.ReadTimer_Tick(Object sender, EventArgs e)
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Can someone please tell me how to fix this   Would adding a third timer that periodically stopped and restarted the other timers every hour fix it   Would I have to dispose of them and recreate them (somehow ). 

TIA 

-Raoul

 



Answer this question

Timer Overflow error

  • ramneek

    I agree with ReneeC, the exception is saying it is happening within your code, in Form1.ReadTimer_Tick().

    To hazard a guess, are you keeping some sort of counter that you're incrementing if you've got an operation that works with it, you might be going out side the bounds of the datatype you're using to store the value. In 24 hours, there's 86400 seconds, and this is much larger than, say, Int16.MaxValue (Int 16 is often seen in VB as 'Short').



  • CrissCross

    Public Class Form1

    Friend WithEvents tmr As New Timer

    Protected num As Integer

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    tmr.Interval = 1

    tmr.Enabled = True

    tmr.Start()

    End Sub

    Private Sub cbGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbGo.Click

    tmr.Enabled = Not tmr.Enabled

    Label1.Text = Val(num)

    End Sub

     

    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick

    num += 1

    End Sub

    End Class

    I ran this for 300,000 cycles which is far more than what you would run in a twenty four period and there was no overflow.

    I rather think the problem is in your code.

     



  • egashira2:50

    Hmm...

     

    I am not intentionally keeping a counter of any sort, but perhaps a by-product.  I am currently on a business trip, but I will study the code as soon as possible to see if I doing just that.  Either way I will report back here.

     

    -Raoul


  • Timer Overflow error