每天在设定的时间运行功能

时间:2017-04-07 09:56:58

标签: vb.net datetime timespan

所以我有一个应用程序,我需要每天在一组时间运行一个电子邮件功能。所以在这种情况下我将其设置为下午6点。我离开了它,下班回家了。下午6点我收到了电子邮件,因此功能已经运行。午夜时分它再次跑了,因为它在午夜运行,而且技术上今天,它现在不会再运行到第二天下午6点,而不是今晚6点,因为它已经在午夜运行。我无法弄清楚出了什么问题,为什么它会在午夜时分运行。

Private Target As DateTime
Private state As MaintenanceState = MaintenanceState.WaitingToStart
Public MaintenanceTime As New TimeSpan(Hour, Min, 0)
Private WaitingInterval As New TimeSpan(0, 5, 0) ' Five minutes

Private Enum MaintenanceState
    WaitingToStart
    Started
End Enum

Private Sub tmr_Maintenance_Tick(sender As Object, e As EventArgs) Handles tmr_Maintenance.Tick
    Dim ts As TimeSpan = Target.Subtract(DateTime.Now)
    lblStatus.Text = ts.ToString("hh\:mm\:ss")
    If DateTime.Now >= Target Then
        tmr_Maintenance.Stop()
        Select Case state
            Case MaintenanceState.WaitingToStart
                SendEmail()
                state = MaintenanceState.Started
                Target = DateTime.Now.Add(WaitingInterval)
            Case MaintenanceState.Started
                If Done = 1 Then
                    Target = DateTime.Now.Add(WaitingInterval)
                Else
                    state = MaintenanceState.WaitingToStart
                    Target = GetNextMaintenanceTarget(MaintenanceTime)
                End If
        End Select
        tmr_Maintenance.Start()
    End If
End Sub

Private Function GetNextMaintenanceTarget(ByVal time As TimeSpan) As DateTime
    Dim dt As DateTime = DateTime.Today.Add(time)
    If DateTime.Now > dt Then
        dt = dt.AddDays(1)
    End If
    Return dt
End Function

3 个答案:

答案 0 :(得分:1)

我实际上建议删除所有与调度有关的代码,而是安排使用任务调度程序运行可执行文件。

答案 1 :(得分:0)

实现目标的可能性稍微容易一点:

Timer Interval设置为默认值100。

Private Sub tmr_Maintenance_Tick(sender As Object, e As EventArgs) Handles tmr_Maintenance.Tick
    tmr_Maintenance.Stop()
    If tmr_Maintenance.Interval = 86400000 Then
        'timer has hit daily run time, run daily code
        SendEmail()

    ElseIf tmr_Maintenance.Interval = 100 Then
        'set interval to next daily run time
        Dim timetillruntime As Integer = DateDiff(DateInterval.Second, New DateTime(Now.Year, Now.Month, Now.Day, 18, 0, 0), Now()) * 1000
        tmr_Maintenance.Interval = timetillruntime

    Else
        'time has hit daily runtime on first day, set timer interval to 24 hours
        tmr_Maintenance.Interval = 86400000
        'run daily code
        SendEmail()

    End If
    tmr_Maintenance.Start()

End Sub

答案 2 :(得分:0)

有趣的问题!

希望此代码段有用

Dim TimeOfBeep as Integer = 18 ' 24 hour format
Dim Target As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, TimeOfBeep , 0, 0, 0, DateTimeKind.Local)
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    TextBox1.Text = Now
    TextBox2.Text = Target
    Dim TDiff = DateDiff(DateInterval.Second, Now, Target) / 60 / 60
    TextBox3.Text = TDiff  ' Time remains for next run

    If (TDiff <= 0) Then
        Beep()
        'sendmail()
        Target.AddHours(24)
    End If
End Sub

如果你可以忍受保存一些CPU工作......你可以设置timer1.interval = 1000 ms OR 60000。

此代码在特定时刻每天发出一次哔声, 如在18:00:00:00的问题

更新:这是一个提示而不是解决方案,它的构造符合一般需求,您可以进行调整以适合您的解决方案。