定时器间隔溢出visual basic

时间:2014-12-22 12:06:07

标签: vb.net vba timer

我正在尝试使用Timer,它可以很好地将其与其他变量一起使用

 Const daily As Integer = 86400000
 Const weekly As Integer = 604800000
 Const hour As Integer = 3600000

但是在使用每月变量Private monthly As Long这里是我的代码

时出现溢出错误
Private timepick As New DateTimePicker()
Private timeLeft As Long
Public trackTimer As Long
Private CloseAllowed As Boolean
Private time As ULong
Private progress As Long
Const daily As Integer = 86400000
Const weekly As Integer = 604800000
Const hour As Integer = 3600000
Private monthly As Long
Private currentDate As Date = DateTime.Now
Private controller As Boolean = False

Private Sub AutoShutDown_Tick(sender As Object, e As EventArgs) Handles AutoShutDown.Tick
    If (controller = False) Then
        AutoShutDown.Enabled = False
    Else
        CountDown.Enabled = True
        progress = 0
        WarningForm.Timer1.Enabled = True
        timeLeft = time
        trackTimer = timeLeft
        AutoShutDown.Interval = time 'Overflow 
        ProgressBar1.Maximum = timeLeft
    End If
    'System.Diagnostics.Process.Start("shutdown", "-s -t 00")
    MsgBox("Shuting down...")
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    timepick.Format = DateTimePickerFormat.Time
    timepick.ShowUpDown = True
    timepick.Location = New Point(154, 12)
    timepick.Width = 123
    Me.Controls.Add(timepick)
    cmbAction.SelectedIndex = 0
    rdoOnce.Checked = True
    Dim days As Long = DateTime.DaysInMonth(currentDate.Year, currentDate.Month)
    monthly = (days * daily)
End Sub

我的Select Case

的一部分
   Case "Monthly"
            Dim dueDate As Date = DateTime.Parse(currentDate.Month & "/" & cmbControl.Text & "/" & currentDate.Year & " " & timepick.Text)
            Dim dayOfTheMonth As Integer = ((dueDate.Day * 24 - currentDate.Hour) * hour) + (dueDate.Minute * 60000) + (dueDate.Hour * hour)
            Dim currentDayOfTheMon As Integer = ((currentDate.Day * 24 - currentDate.Hour) * hour) + (currentDate.Minute * 60000) + (currentDate.Hour * hour)
            If (dayOfTheMonth > currentDayOfTheMon) Then
                timeLeft = (dayOfTheMonth - currentDayOfTheMon)
            End If
            time = monthly
            controller = True
    End Select

    trackTimer = timeLeft
    ProgressBar1.Maximum = timeLeft
    AutoShutDown.Interval = timeLeft
    AutoShutDown.Enabled = True
    CountDown.Enabled = True

1 个答案:

答案 0 :(得分:2)

  

但使用每月变量

时出现溢出错误

这是一个很好的问题,它告诉你你的代码很糟糕。 Timer的Interval属性是一个Integer,单位是毫秒。一个月是31 x 24 x 3600 x 1000 = 2,678,400,000毫秒。这比Integer.MaxValue,2,147,483,648更多。或者换句话说,你不能使用计时器使用毫秒来超过24天。

一个简单的解决方法是不要计算整个时间段,而是让你的计时器每小时打勾一次。实际间隔无关紧要,只需检查是否有足够的时间过去。大致是:

Private ShutdownDate As DateTime

Private Sub StartShutdownTimer(ByVal interval As Long)
    ShutdownDate = DateTime.UtcNow.AddMilliseconds(interval)
    AutoShutDown.Enabled = True
End Sub

Private Sub AutoShutDown_Tick(sender As Object, e As EventArgs)
    If DateTime.UtcNow >= ShutdownDate Then
        AutoShutDown.Enabled = False
        '' etc...
    End If
End Sub