Results 1 to 9 of 9

Thread: [RESOLVED] increase timer interval

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2006
    Location
    Jerusalem
    Posts
    39

    Resolved [RESOLVED] increase timer interval

    hi

    the max interval that can be used for a timer is about 64,000 milliseconds wich is about a minute.. is there a way to increase that like using multiple intervals or something ?

    thnx

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up Re: increase timer interval

    You can not able to increase the timer interval but you can use the counter for it. Use an integer variable and count it and whenever it get a specific number then you can use the timer.

  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: increase timer interval

    And here's how (just change myInterval to whatever seconds you'd like):
    VB Code:
    1. Option Explicit
    2.     Private myInterval As Integer
    3.  
    4. Private Sub Form_Load()
    5.     myInterval = [B]180[/B] '3 minutes for example
    6.  
    7.     With Timer1
    8.         .Interval = 1000 'a second
    9.         .Enabled = True
    10.     End With
    11. End Sub
    12.  
    13. Private Sub Timer1_Timer()
    14.     Static seconds As Integer
    15.         seconds = seconds + 1
    16.             If seconds = myInterval Then
    17.                 seconds = 0
    18.                     Debug.Print "Interval"
    19.             End If
    20. End Sub

  4. #4
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: increase timer interval

    CS

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: increase timer interval

    I know this is more code, but it is also more precise, and also the timer does not execute very often (like every second)

    ALSO... the event will always fire at the same interval (MyInterval) even if the code in the event takes a long time (but less than MyInterval)
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTickCount Lib "kernel32" () As Long
    4.  
    5. Private myInterval As Long, StartTick As Long
    6. Private Const MaxTimerInterval As Long = 65000 ' Actually max interval is 65535
    7.  
    8. Private Sub Form_Load()
    9.     ' Set the interval
    10.     myInterval = 180 * 1000&  ' 180 seconds, 3 minutes...
    11.    
    12.     StartTick = GetTickCount
    13.     Timer1.Enabled = True
    14.     Timer1.Interval = 1
    15. End Sub
    16.  
    17. Private Function lngMIN(N1 As Long, N2 As Long) As Long
    18.     If N1 < N2 Then
    19.         lngMIN = N1
    20.     Else
    21.         lngMIN = N2
    22.     End If
    23. End Function
    24.  
    25. Private Sub Timer1_Timer()
    26.     Dim TimeLeft As Long
    27.     TimeLeft = myInterval - (GetTickCount - StartTick)
    28.    
    29.     If TimeLeft <= 0 Then
    30.         StartTick = GetTickCount
    31.         TimerEvent  ' Execute the timer event
    32.         TimeLeft = myInterval - (GetTickCount - StartTick)
    33.     End If
    34.    
    35.     Timer1.Interval = lngMIN(TimeLeft, MaxTimerInterval)
    36. End Sub
    37.  
    38. Private Sub TimerEvent()
    39.     ' Put your code to execute at timer event
    40. End Sub
    Last edited by CVMichael; Nov 28th, 2006 at 10:56 AM.

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: increase timer interval

    or you could use an API timer - which has a lot higher limit.

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: increase timer interval

    Quote Originally Posted by bushmobile
    or you could use an API timer - which has a lot higher limit.
    But then you have to subclass your form, which is a lot more code, and comes with other problems, like crashing your program unexpectedly

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: increase timer interval

    Quote Originally Posted by CVMichael
    But then you have to subclass your form, which is a lot more code, and comes with other problems, like crashing your program unexpectedly
    you don't need to subclass your program - you pass the address of a sub to SetTimer and any WM_TIMER messages are passed through it (you can also process the WM_TIMER messages through WndProc, but you don't have to)

    and it doesn't crash your program unexpectantly - it crashes it when it hits unhandled errors - if your program is running fine then any subclassing / API timer / etc. won't cause you any trouble - just gotta remember to save often

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2006
    Location
    Jerusalem
    Posts
    39

    Re: increase timer interval

    thank you guys ..
    you have been helpful : )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width