locked
Timer's Interval RRS feed

  • Question

  • hi i m using

    System.Timers.Timer()

    wats the exact interval code for 1/2 an hour

    i set

    Dim

     

    aTimer As New System.Timers.Timer()

     

    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

    aTimer.Interval = 60000 * 30

    aTimer.Enabled =

    True

    but the timer event is fired after 44 minutes
    anyone can give exact number for half an hour

    Tuesday, February 2, 2010 10:11 AM

Answers

  • Any reason that you this timer for a Windows Service program are using.

    The windows.forms.timer is easier to use and in most cases more acurate.


    Success
    Cor
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 11:41 AM
  • dim sec1 as integer = 1000
    dim min1 as integer = sec1  * 60
    dim min30 as integer = min1 * 30

    or
    dim min30 as integer = 1000*60*30
     

    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    Tuesday, February 2, 2010 12:16 PM
  • The following program yielded these results:

    Elapsed time 0:30:00
    Press any key to continue . . .


    Perhaps you can use this code as a model for what you're trying to accomplish.

    Imports System.Timers
    
    Module Module1
    
    Private Const MIN30 As Integer = 1800000
    Private t0 As Date = Nothing
    Private aTimer As Timer = Nothing
    Private bStop As Boolean = False
    
    Sub Main()
    	aTimer = New Timer
    	AddHandler aTimer.Elapsed, AddressOf OnTimerEvent
    	aTimer.Interval = MIN30
    	aTimer.Enabled = True
    
    	t0 = Now
    
    	Do Until bStop
    		Threading.Thread.Sleep(250)
    	Loop
    
    	aTimer.Enabled = False
    
    	Console.WriteLine(Environment.NewLine & "Press any key to continue . . .")
    	Console.ReadKey()
    End Sub
    
    Public Sub OnTimerEvent(ByVal sender As Object, ByVal e As ElapsedEventArgs)
    	Dim ts As TimeSpan = Now - t0
    	Console.WriteLine("Elapsed time {0}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)
    	bStop = True
    End Sub
    
    End Module
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 1:54 PM
  • dim sec1 as integer = 1000
    dim min1 as integer = sec1  * 60
    dim min30 as integer = min1 * 30

    or
    dim min30 as integer = 1000*60*30
     

    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774

    Hi sivakl_2001,

    I have proposed the above answer.

    The total is for 30 minutes is 1800000 ( 18 followed by 5 zeroes )

    as there are 1000 milliseconds in a second, which is the smallest interval for a Timer.


    Regards,

    John
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 4:08 PM
  • This looks much easier to me

    Module Module1
        Public WithEvents mytimer As New System.Windows.Forms.Timer With {.Enabled = True, .Interval = 1800000}
        Sub main()
            mytimer.Start()
        End Sub
    
        Private Sub mytimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles mytimer.Tick
            Windows.Forms.MessageBox.Show("I'm there")
        End Sub
    End Module
    





    Success
    Cor
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 6:53 PM
  • There's something wrong with your system timing or your stopwatch.  1000 * 60 * 30 or 60000 * 30 or 1800000 are all the same and should all come out at 30 minutes. 

    Are you quite sure that there's not something else that could be triggering the timer event, or the tier is being stopped or diabled from some other part of the program?
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 10:27 PM

All replies

  • Any reason that you this timer for a Windows Service program are using.

    The windows.forms.timer is easier to use and in most cases more acurate.


    Success
    Cor
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 11:41 AM
  • dim sec1 as integer = 1000
    dim min1 as integer = sec1  * 60
    dim min30 as integer = min1 * 30

    or
    dim min30 as integer = 1000*60*30
     

    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    Tuesday, February 2, 2010 12:16 PM
  • The following program yielded these results:

    Elapsed time 0:30:00
    Press any key to continue . . .


    Perhaps you can use this code as a model for what you're trying to accomplish.

    Imports System.Timers
    
    Module Module1
    
    Private Const MIN30 As Integer = 1800000
    Private t0 As Date = Nothing
    Private aTimer As Timer = Nothing
    Private bStop As Boolean = False
    
    Sub Main()
    	aTimer = New Timer
    	AddHandler aTimer.Elapsed, AddressOf OnTimerEvent
    	aTimer.Interval = MIN30
    	aTimer.Enabled = True
    
    	t0 = Now
    
    	Do Until bStop
    		Threading.Thread.Sleep(250)
    	Loop
    
    	aTimer.Enabled = False
    
    	Console.WriteLine(Environment.NewLine & "Press any key to continue . . .")
    	Console.ReadKey()
    End Sub
    
    Public Sub OnTimerEvent(ByVal sender As Object, ByVal e As ElapsedEventArgs)
    	Dim ts As TimeSpan = Now - t0
    	Console.WriteLine("Elapsed time {0}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)
    	bStop = True
    End Sub
    
    End Module
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 1:54 PM
  • dim sec1 as integer = 1000
    dim min1 as integer = sec1  * 60
    dim min30 as integer = min1 * 30

    or
    dim min30 as integer = 1000*60*30
     

    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774

    Hi sivakl_2001,

    I have proposed the above answer.

    The total is for 30 minutes is 1800000 ( 18 followed by 5 zeroes )

    as there are 1000 milliseconds in a second, which is the smallest interval for a Timer.


    Regards,

    John
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 4:08 PM
  • This looks much easier to me

    Module Module1
        Public WithEvents mytimer As New System.Windows.Forms.Timer With {.Enabled = True, .Interval = 1800000}
        Sub main()
            mytimer.Start()
        End Sub
    
        Private Sub mytimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles mytimer.Tick
            Windows.Forms.MessageBox.Show("I'm there")
        End Sub
    End Module
    





    Success
    Cor
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 6:53 PM
  • There's something wrong with your system timing or your stopwatch.  1000 * 60 * 30 or 60000 * 30 or 1800000 are all the same and should all come out at 30 minutes. 

    Are you quite sure that there's not something else that could be triggering the timer event, or the tier is being stopped or diabled from some other part of the program?
    • Marked as answer by Alex Liang Tuesday, February 9, 2010 1:28 PM
    Tuesday, February 2, 2010 10:27 PM