Hey,
I've been trying to get this part of my code to work but it hasn't done anything. I'm trying to create a function that sends an email everyday at a specific time. I thought threading.timer would work but i haven't figured it out yet. I don't get errors when its executed but it also does nothing. If someone could tell me whats going on with this piece of code, i would greatly appreciate it. Thanks
I've been trying to get this part of my code to work but it hasn't done anything. I'm trying to create a function that sends an email everyday at a specific time. I thought threading.timer would work but i haven't figured it out yet. I don't get errors when its executed but it also does nothing. If someone could tell me whats going on with this piece of code, i would greatly appreciate it. Thanks
Code:
Shared timer As System.Threading.Timer
Public Sub Main(ByVal args As String())
'get today's date at 9:00 AM
Dim nineAM As DateTime = DateTime.Today.AddHours(9)
'if 9:00AM has passed, get tomorrow at 9:00 AM
If DateTime.Now > nineAM Then
nineAM = nineAM.AddDays(1)
End If
' calculate milliseconds until the next 9:00AM
Dim FirstExecution As Integer = CInt(Math.Truncate(nineAM.Subtract(DateTime.Now).TotalMilliseconds))
' calculate the number of milliseconds in 24 hours
Dim TimeSpan As Integer = CInt(Math.Truncate(New TimeSpan(24, 0, 0).TotalMilliseconds))
'set the method to execute when the time executes
Dim ExecuteEmail As TimerCallback = AddressOf Email
'start the timer. The timer will execute "Email" when the number of seconds between now
'the next 9:00 AM elapse. After that, it will execute every 24 hours
Dim timer As New System.Threading.Timer(ExecuteEmail, Nothing, FirstExecution, TimeSpan)
'Block the main thread forever. The timer will continue to execute
Thread.Sleep(Timeout.Infinite)
End Sub
Comment