0

我需要在整点半小时执行命令行。那么有没有更好的方法来做到这一点呢?也许不涉及每秒检查小时的那些。

间隔为 1 秒的计时器:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        For i As Integer = 0 To 24
            If TimeString = i & ":00:00" & i Then or TimeString = "0" & i & ":00:00 or If TimeString = i & "30:00:" & i Then or TimeString = "0" & i & ":30:00

            End If
        Next
End Sub
4

2 回答 2

1

步骤 1 - 计算从现在到下一个小时或半小时标记的时间;

第 2 步 - 将计时器的经过时间设置为等于第 1 步中计算的时间;

第 3 步 - 当计时器计时,将经过的时间重置为 30 分钟,然后做你需要做的工作。

如果该过程必须精确地在一小时/半小时内运行,请重新计算步骤 3 中所需的时间,而不是将其设置为 30 分钟(这将补偿漂移)。

这是一些计算到午夜的毫秒数的代码;你应该可以从那里工作

Private Function MillisecondsToMidnight() As Integer

    Dim ReturnValue As Integer
    Dim ts As TimeSpan

    Dim Tomorrow As DateTime = Today.AddDays(1)
    ts = Tomorrow.Subtract(Now)

    ReturnValue = ts.TotalMilliseconds()

    ts = Nothing
    Return ReturnValue

End Function
于 2013-05-14T12:05:55.860 回答
0

你的问题真的很有趣。我认为现在所需的功能已在下面完全实现。我测试了它,我相信它有效。

假设您使用 Button1 触发整个功能,并且您想使用我们拥有的 Timer 组件:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim seconds As New Integer()
    Dim minutes As New Integer()
    seconds = System.DateTime.Now.TimeOfDay.Seconds
    minutes = System.DateTime.Now.TimeOfDay.Minutes
    Dim firstOccurrence As TimeSpan = TimeSpan.Zero
    Do
        If seconds.Equals(60) Then
            Exit Do
        Else
            seconds = seconds + 1
            firstOccurrence = firstOccurrence + TimeSpan.FromSeconds(1)
        End If
    Loop

    Do
        If minutes.Equals(59) Or minutes.Equals(29) Then
            Exit Do
        Else
            minutes = minutes + 1
            firstOccurrence = firstOccurrence + TimeSpan.FromMinutes(1)
        End If
    Loop

    Timer1.Interval = (((firstOccurrence.Minutes) * 60) + ((firstOccurrence.Seconds))) * 1000
    Timer1.Enabled = True


End Sub 

Private Sub Timer1_Tick(sender As Object, e As EventArgs) 处理 Timer1.Tick

    'execute your code here
    Timer1.Interval = 30 * 60 * 1000

End Sub
于 2013-05-14T20:19:44.413 回答