We help IT Professionals succeed at work.
Get Started

System.Threading.Timer Not Using the Specified Span Time Between Call Backs

bobcann
bobcann asked
on
405 Views
Last Modified: 2012-05-09
The problem is it does not set the spanof once a day using TimeSpan. The debug window displays the correct span of one day, but it runs every two hours:

-            Interval      {System.TimeSpan}      System.TimeSpan
            Days      1      Integer
            Hours      0      Integer
+            MaxValue      {System.TimeSpan}      System.TimeSpan
            Milliseconds      0                  Integer
            Minutes      0      Integer
+            MinValue      {System.TimeSpan}      System.TimeSpan
            Seconds      0      Integer
            Ticks      864000000000                     Long
            TicksPerDay      864000000000            Long
            TicksPerHour      36000000000            Long
            TicksPerMillisecond      10000            Long
            TicksPerMinute      600000000                  Long
            TicksPerSecond      10000000            Long
            TotalDays      1.0            Double
            TotalHours      24.0               Double
            TotalMilliseconds      86400000.0      Double
            TotalMinutes      1440.0                  Double
            TotalSeconds      86400.0                  Double
+            Zero      {System.TimeSpan}            System.TimeSpan

It works for hours but not days. The start time is perfect and I suppose one day can be specified as 24 hours, but I'd rather get this working correctly. Any ideas?

Thanks for your help.
Imports System.Threading

Module mdlTimer
    Private Timer As System.Threading.Timer = Nothing
    Dim clsPathNames As frmPathNames

    Public Sub StartTimer()
        Dim intMinutessToStartOfTimer As Integer = GetMinutessToStartOfTimer()
        Dim StartTime As New TimeSpan(0, intMinutessToStartOfTimer, 0)
        Dim Interval As TimeSpan

        Select Case strFrequency
            Case "Once a Day"
                Interval = New TimeSpan(1, 0, 0, 0) 'one day

            Case "Even Few Hours"
                Interval = New TimeSpan(byteIntervalTime, 0, 0) 'hours

            Case "Every Few Days"
                Interval = New TimeSpan(byteIntervalTime, 0, 0, 0) 'days
        End Select

        Timer = New System.Threading.Timer(New TimerCallback(AddressOf DeleteFiles), Nothing, StartTime, Interval)
    End Sub

    Public Function GetMinutessToStartOfTimer() As Integer
        Const cMidnight As Integer = 24
        Const cNoon As Integer = 12
        Dim intCurrentHour As Integer = DatePart(DateInterval.Hour, Now)
        Dim intHourInMinutes As Integer = intCurrentHour * 60
        Dim intMinute As Integer = DatePart(DateInterval.Minute, Now)
        Dim intMinutesToStartOfTimer As Integer = intMinute

        Dim intStartTime As Integer = CInt(mdlManager.strStartTime.Remove(2)) '* trim ":00")
        intStartTime = CInt(intStartTime)

        If intStartTime = 0 Then intStartTime = cMidnight

        If intStartTime < cNoon And intCurrentHour < cMidnight Then '* if start time delay crosses midnight
            Dim intPreMidnightDelayHours As Integer = cMidnight - intCurrentHour
            Dim intPreMidnightDelayMinutes As Integer = intMinute

            Dim intPreMidnightDelay As Integer = (intPreMidnightDelayHours * 60) - intPreMidnightDelayMinutes
            Dim intPostMidnightDelay As Integer = intStartTime * 60

            intMinutesToStartOfTimer = intPostMidnightDelay + intPreMidnightDelay
        Else
            intMinutesToStartOfTimer = intHourInMinutes + intMinute
            intMinutesToStartOfTimer = CLng((CInt(intStartTime) * 60) - intMinutesToStartOfTimer)
            intMinutesToStartOfTimer = IIf(intMinutesToStartOfTimer >= 0, intMinutesToStartOfTimer, -intMinutesToStartOfTimer)
        End If

        Return intMinutesToStartOfTimer
    End Function

    Private Sub DeleteFiles(ByVal state As Object)
        Console.WriteLine("Timer Callback: " & Now.ToString)
        clsFilePaths.BuildFilePaths()
        mdlManager.DeletedFiles()
    End Sub
End Module

Open in new window

Comment
Watch Question
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE