vb.net 2010中的计时器问题

时间:2014-03-05 15:56:03

标签: vb.net timer

我希望调用一个计时器来删除vb.net中数据库的记录。我有一个计时器,它将根据用户提供给textbox1的输入值调用。用户可以选择1到6之间的几天后,数据库记录将被删除。我做了getInterval函数来延迟timer2的启动“用户输入的值为1-6天”。我从网上获得了代码然后修改了它。但它的不稳定。它生成了一个“DivideByZero异常”。总之我只有希望将timer2的启动延迟1-6天,具体取决于用户的选择。请帮助我。谢谢,这是代码

Private Function getInterval(ByVal Days As Integer) As Integer

    Dim now As DateTime = DateTime.Now

    ' duplicate the current time for use as a working variable
    Dim later As DateTime = now
    Dim interval As TimeSpan

    ' Add minutes contained in days to the target time
    later = later.AddDays(CDbl(Days))

    'subtract the modulo to get the target time days value
    later = later.AddDays(CInt(-later.Day Mod Days))

    ' zero out the seconds in the target time by subtracting the seconds value
    ' from the target time
    later = later.AddMinutes(-later.Minute)


    ' zero out the seconds in the target time by subtracting the seconds value
    ' from the target time
    later = later.AddSeconds(-later.Second)

    ' calculate the interval
    interval = later - now

    ' convert the interval to milliseconds and return
    ' use this interval to set a timer value to trigger at the
    ' desired time.
    getInterval = CInt((interval.Days * 86400000) + (interval.Hours * 3600000) + (interval.Minutes * 60000) + (interval.Seconds * 1000))

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Interval = getInterval(Val(TextBox1.Text))
End Sub

0 个答案:

没有答案