重新启动文件名迭代

时间:2014-05-14 13:25:50

标签: vb.net timer

我想在系统时钟到达00:00或12:00 MN时重启迭代。我从this (below)链接的答案中得到了迭代代码,它完美无缺。

Public Sub GetLastNumber(ByVal filePath As String)
    Dim lastFileNo As Integer = 1
    Dim files() As String = Directory.GetFiles(filePath, "*.txt")

    For Each file As String In files
        file = Path.GetFileNameWithoutExtension(file)
        Dim numbers As MatchCollection = Regex.Matches(file, "(?<num>[\d]+)")

        For Each number In numbers
            number = CInt(number.ToString())
            If number > 0 And number < 1000 And number > lastFileNo Then lastFileNo = number
        Next
  lastnumber.Text = number
    Next
End Sub

我偶然发现了使用Timer这样的one below的内容,但它给出了一个错误,指出转换失败“AM”为StringDate类型。< / p>

Public Sub DoStuff(ByVal obj As Object)
    MessageBox.Show("It's already time", "TIME!", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

Public Sub testT()
    Dim tcb As TimerCallback = AddressOf DoStuff
    Dim t As Timer
    Dim execTime As TimeSpan
    Dim dtNow As DateTime = DateTime.Now
    Dim hc As Integer = 12
    Dim mc As Integer = 0

    If TimeOfDay.ToString("tt").Contains("AM") And hc = 12 Then
        hc = 0
    ElseIf TimeOfDay.ToString("tt").Contains("PM") Then
        hc = 12 + (12 - hc)
        If hc = 24 Then
            hc = 0
        End If
    End If

    Dim dtCandidate As DateTime = New DateTime(dtNow.Year, dtNow.Month, dtNow.Day, hc, mc, 0)

    If dtCandidate < dtNow Then
        dtCandidate.AddDays(1)
    End If

    execTime = dtNow.Subtract(dtCandidate)
    resultBox.Text = execTime.ToString
    t = New Timer(tcb, Nothing, execTime, TimeSpan.Zero)
End Sub

Public Sub realTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)    Handles realTime.Tick
    TimeNow.Text = TimeOfDay.ToString("HH:mm:ss")
    testT()
End Sub

使用TimeOfDay.ToString("tt").Contains("AM/PM")补救了转换失败。通过更正ElseIf语句来纠正无法表示的DateTime 错误。由于没有更多错误,我试图将testT函数置于Timer点火中1000毫秒。系统时钟到达午夜(00:00)后,DoStuff功能的消息框在午夜后每秒显示一次。如何停止这种情况,但下次时钟到达午夜时仍能显示出来?

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

你为计时器链接的代码是非常糟糕的,因为它试图在操作DateTimes时使用字符串 - 它们是非常不同的东西。

我创建了一个新的Windows窗体应用程序,其中只有一个名为&#34; TimeNow&#34;的标签:

Public Class Form1

    Friend WithEvents realTime As Windows.Forms.Timer
    Private lastAlarmTime As DateTime
    Private alarmTimes As List(Of DateTime)
    Private displayTime As String

    Public Sub DoStuff()
        MessageBox.Show("It's already time", "TIME!", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Public Sub CheckForAnAlarmTime()
        Dim dtNow As DateTime = DateTime.Now()

        For Each tt In alarmTimes
            ' the timer interrupt handler is not necessarily called at exact times. Allow for that.
            If tt > lastAlarmTime AndAlso tt < dtNow Then
                lastAlarmTime = dtNow
                DoStuff()
                SetAlarmTimes()
                Exit For
            End If
        Next

    End Sub

    Public Sub realTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles realTime.Tick
        Dim candidateDisplayTime As String = DateTime.Now.ToString("HH:mm:ss")
        ' only update the UI if necessary
        If candidateDisplayTime <> displayTime Then
            displayTime = candidateDisplayTime
            TimeNow.Text = displayTime
        End If

        CheckForAnAlarmTime()

    End Sub

    Private Sub SetAlarmTimes()
        Dim dtNow As DateTime = DateTime.Now()

        alarmTimes = New List(Of DateTime)
        alarmTimes.Add(New DateTime(dtNow.Year, dtNow.Month, dtNow.Day, 12, 0, 0))
        ' Recommendation: do not use exactly midnight without extensive testing, i.e. test over day rollover, month rollover, and year rollover.
        ' With less testing, use a few milliseconds less than midnight.
        alarmTimes.Add(New DateTime(dtNow.Year, dtNow.Month, dtNow.Day, 0, 0, 0).AddMilliseconds(-50))

    End Sub

    Private Sub SetUpAlarmsTimer()
        SetAlarmTimes()
        lastAlarmTime = DateTime.Now()
        realTime_Tick(Me, EventArgs.Empty)
        realTime = New Windows.Forms.Timer()
        realTime.Interval = 200 ' 200ms will update it more accurately w.r.t. visual appearance
        AddHandler realTime.Tick, AddressOf realTime_Tick
        realTime.Start()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SetUpAlarmsTimer()

    End Sub

End Class

将alarmTimes更改为您需要检查每个alarmTimes项目仅引发一次警报的任何内容。

我不愿意等到午夜才能检查alarmTimes.Add(New DateTime(dtNow.Year, dtNow.Month, dtNow.Day, 0, 0, 0) .AddMilliseconds(-50)是否能按要求工作,或者直到月末或年末绝对当然。请不要忘记2月底闰年时的测试。

检查lastAlarmTime的原因是它不确定何时会引发一个计时器事件:对于设置为1000ms的计时器,你可能在一个实际秒内获得两个事件,或者在一个真实的秒内没有。约。

编辑:您可能还希望使用UTC工作,以避免日光时间变化的麻烦。