So based on Dave’d and Julian’s suggestions/comments, here is what I will be trying. If you have criticism for something I’ve missed or suggestions to improve it I’m open to your feedback.
Function that calculates the total second of the next hour on the hour
Public Function GetNextHourTotalSeconds() as Double
dim d As Date = New Date
d.Hour = d.Hour + 1
d.Minute = 0
d.Second = 0
Return d.TotalSeconds
End Function
Function that calculates the period to set the timer for the next hour on the hour action Event to Trigger
Public Function GetNextBatchPeriodMilliseconds() as Integer
Dim d As Date = New Date
Dim nextmilliseconds As Integer
nextmilliseconds = (GetNextHourTotalSeconds - d.TotalSeconds) * 1000
Return nextmilliseconds
End Function
In the App.Open Even I call a Method called ProcessingStart() Among other things it creates two new timer objects, BatchPeriodTask and PollPeriodTask. BatchPeriodTask sets the Boolean variable BatchRun to True every hour so long as it is already False. PollPeriodTask executes a thread every 10 seconds so long as it is not already running
[code]Public Sub ProcessingStart()
BatchPeriodTask = New BatchPeriodTimer
BatchNextHourTotalSeconds = GetNextHourTotalSeconds
BatchPeriodTask.Period = GetNextBatchPeriodMilliseconds
BatchPeriodTask.Mode = 2
PollPeriodTask = New PollPeriodTimer
PollPeriodTask.Period = DBPollingFrequency
PollPeriodTask.Mode = 2
AppPaused = False
LogTransactions(“Started processing queued jobs”)
End Sub[/code]
Timer BatchPeriodTask Action Event sets the Boolean variable BatchRun to True every hour so long as it is already False. If the timer fires before the the current total seconds is greater than or equal to the variable BatchNextHourTotalSeconds then it sets the period to wait 10 more seconds
Sub Action() Handles Action
Dim d As Date = New Date
If d.TotalSeconds >= BatchNextHourTotalSeconds
If BatchRun = False
BatchRun = True
End If
BatchNextHourTotalSeconds = GetNextHourTotalSeconds
Me.Period = GetNextBatchPeriodMilliseconds
Else
Me.Period = 10000
End If
End Sub
Finally in the Thread that runs every 10 seconds which the Run Event checks for a number of status changes in a database and also checks various Boolean variables, and processes changes when they have occurred, I look for the value of the variable BatchRun to be True and if so do some Batch stuff.
Sub Run() Handles Run
Dim b As Boolean
Dim sqlString,desc,err As String
MyAppThreadTaskRunning = True
b = DBConnect(MySQLDBName)
If b = True Then
//----------------------------------------------------------------------------------------
// Do some stuff
//----------------------------------------------------------------------------------------
If BatchRun = True
// Do the batch Processing stuff
BatchRun = False
End If
//----------------------------------------------------------------------------------------
DBClose
Else
desc = "MyAppThreadTThread.DBConnect.Error"
err = ": " + "The MyAppThread Returned a DB connection Error"
// Log the Exception
LogExceptions(desc+err)
// Send a Notification Email
SendEmail(desc + err + " - Processing Jobs was aborted." ,"MyAppThread." + desc,"text")
End If
MyAppThreadTaskRunning = False
End Sub
Thanks for the help guys.