I wanted to restart the iteration when the system clock hits 00:00 or 12:00 MN. I got the iteration code from the answer of this (below) link, and it works perfectly.
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
I stumbled something that uses a Timer
like this one below, but it's giving me an error saying conversion fail "AM" as String
to a Date
type.
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
Conversion failure was remedied by using TimeOfDay.ToString("tt").Contains("AM/PM")
. Un-representable DateTime error was remedied by correcting the ElseIf
statements. Since there's no more error, I tried to put the testT
function inside a Timer
firing at 1000 ms. After system clock hit midnight(00:00), the message box of the DoStuff
function showed every second after midnight. How can this be stopped but can still show up the next time the clock hits midnight?
Can somebody help me out?