Vb.net 重新启动文件名迭代

Vb.net 重新启动文件名迭代,vb.net,timer,Vb.net,Timer,我想在系统时钟达到00:00或12:00MN时重新启动迭代。我从link的答案中得到了迭代代码,它工作得非常完美 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

我想在系统时钟达到00:00或12:00MN时重新启动迭代。我从link的答案中得到了迭代代码,它工作得非常完美

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
通过使用
TimeOfDay.ToString(“tt”).Contains(“AM/PM”)
修复了转换失败。通过更正
ElseIf
语句,纠正了不可表示的日期时间错误。由于没有更多的错误,我尝试将
testT
函数放入
计时器中,计时器以1000毫秒的速度启动。在系统时钟达到午夜(00:00)后,
DoStuff
函数的消息框在午夜后每秒显示一次。这怎么能停止,但仍然可以出现在下一次时钟敲响午夜


有人能帮我吗?

你链接的计时器代码非常糟糕,因为它试图在处理日期时间时使用字符串-它们是非常不同的东西

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

公共类表单1
Friend WithEvents realTime作为Windows.Forms.Timer
私有lastAlarmTime作为日期时间
私有报警时间作为列表(日期时间)
私有显示时间作为字符串
公共子DoStuff()
Show(“时间到了”,“时间到了!”,MessageBoxButtons.OK,MessageBoxIcon.Information)
端接头
NalarmTime()的公共子检查
Dim dtNow As DateTime=DateTime.Now()
对于报警时间内的每个tt
'计时器中断处理程序不一定在精确的时间调用。考虑到这一点。
如果tt>lastAlarmTime,并且tt
将alarmTimes更改为您需要检查的任何值,以确保每个alarmTimes项目仅引发一次报警

我不愿意等到午夜才检查
alarmTimes.Add(新日期时间(dtNow.Year,dtNow.Month,dtNow.Day,0,0)
而不检查
.addmillizes(-50)
将按要求工作,或者直到月底或年底才能完全确定。请不要忘记在闰年的2月底左右进行测试

检查lastAlarmTime的原因是不确定何时会引发计时器事件:对于设置为滴答1000毫秒的计时器,您可能会在一个实秒内收到两个事件,或者在一个实秒内没有。大约


编辑:您可能还希望在UTC工作,以避免夏令时更改带来的麻烦。

该错误的意思是-
TimeOfDay
返回一个日期类型(例如
\08:33:34 AM
),您正在将其与字符串类型(例如“AM”)进行比较。即使你可以比较,它们也永远不会相等。一些关于数据类型的研究似乎是有序的。问题的另一半是什么?你有工作报警代码和工作文件迭代代码。这是表单[design]上的一个错误。如果我删除名为“Button1”的按钮,例如表单[design]显示“Button1”已被删除。但是,当我开始运行/调试它时,“Button1”仍然存在。
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
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