Hey,
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. The program will be put in a windows scheduler.
Thanks
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. The program will be put in a windows scheduler.
Thanks
VB.NET:
Private Shared Sub Main(ByVal args As String())
'get today's date at 9:00 AM
Dim nineAM As DateTime = DateTime.Today.AddHours(11)
'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(0, 2, 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