Trying to get Threading.Timer to do a function everyday at a specific

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aggie88
    New Member
    • Jun 2010
    • 1

    #1

    Trying to get Threading.Timer to do a function everyday at a specific

    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

    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
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Ok, hmmm. And you have verified that the numbers are correct, and left the program running the entire time it needs to? (Because shutting it down and starting it up would make it "skip" probably)

    How about making the timer execute every 30secs and checking the time inside the timer's function?

    Comment

    Working...