Problem with Timer object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Problem with Timer object

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    49

    Problem with Timer object

    Hello,

    Have aproblem with the timer object. I guess everyone knows that Timer objects are not very precise, that is, if I want the timer to run for say 1min of actual time with 100milliseconds interval, it will only run for about 58000 milliseconds of the actual 1 min. there is a difference of about 2 seconds(not sure but there is diference) between the actual time and the timer time.
    Is there any way I could correct this or write a timer function using an Api such as timeGetTime etc.

    thanks

  2. #2
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    is not that the Timer is not precise, it is because Windows is not a real time operative system and VB is strictly synchronous. For example if the app is in the middle of a busy operation the Timer will fire only when it is finished. You can set an interval that is smaller than your desired time, then in the Timer event use the DateDiff to compare the current time with the start time, or use the GetTickCount or timeGetTime API, and take action only the interval is really expired.
    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  3. #3
    Join Date
    Dec 2003
    Posts
    2,750
    You might want to take a look at the CCRP High Performance Timer Objects:

    http://ccrp.mvps.org/download/ccrpdownloads.htm
    Paul
    ~~~~
    Microsoft MVP (Visual Basic)

  4. #4
    Join Date
    Mar 2005
    Posts
    49
    Hi Mstraf, I did what you said, I tried to take the difference between timeGetTime...but the code doesnt do what i intend it to do. I just used one timer on the form with interval fo 10ms. My code is as shown below,


    Code:
    Private Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
    Private Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
    Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    
    Private time_array() As Long
    Public Cnt As Double
    Public start_time As Double
    Public i As Double
    
    
    Private Sub Form_Load()
    timeBeginPeriod 1
    start_time = timeGetTime
    Cnt = 1
    i = 0
    End Sub
    
    Private Sub Timer1_Timer()
    
    Dim timediff As Long
    
    timediff = (timeGetTime - start_time)
    
    If (timediff Mod 100 = 0 And timediff <= 10000) Then
    
        ReDim Preserve time_array(Cnt)
        time_array(Cnt) = 100 + i
        
        Print (time_array(Cnt))
        i = time_array(Cnt)
        Cnt = Cnt + 1
        
    ElseIf (timediff >= 10000) Then
    
        Timer1.Enabled = False
        Print ("done")
    
    End If
    
    End Sub
    The expected result is : 100, 200, 300,.........10,000

    Please help me out.

    Thanks
    Last edited by teja8100; 05-17-2005 at 11:40 AM.

  5. #5
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    I guess the problem is in this test:

    if timediff Mod 100 = 0 ....

    probably timeDiff will not always be a multiple of 100 (likely it will be 105, 297, 302 and so on)
    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  6. #6
    Join Date
    Mar 2005
    Posts
    49
    is there anyway i could do this without using the Timer object...i mean using an infinite loop. I want to have a parallel processing like the timer object, can i achieve that using an infinite loop?

    I have modified the timediff test, and it works fine, but stil have a difference of 100ms.

    Code:
    Private Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
    Private Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
    Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    
    Private time_array() As Long
    Public Cnt1 As Double
    Public start_time As Double
    Public i As Double
    Public prevtimediff As Long
    
    
    
    Private Sub Form_Load()
    timeBeginPeriod 1
    start_time = timeGetTime
    Cnt1 = 1
    i = 0
    prevtimediff = 0
    End Sub
    
    Private Sub Timer1_Timer()
    
    Dim timediff As Long
    
    
    timediff = (timeGetTime - start_time)
    
    
    If (Floor(timediff / 100) = (Floor(prevtimediff / 100) + 1) And timediff <= 180000) Then
    
    'Print (timediff)
    
        prevtimediff = timediff
        ReDim Preserve time_array(Cnt1)
        time_array(Cnt1) = 100 + i
        
        'Print (time_array(Cnt1))
        i = time_array(Cnt1)
        Cnt1 = Cnt1 + 1
        
        
    ElseIf (timediff >= 180000) Then
    
        Timer1.Enabled = False
        Print ("done")
        Print (time_array(Cnt1 - 1))
        Print (timediff)
    End If
    
    End Sub

    The Floor function:
    Code:
    Public Function Floor(num As Double) As Double
    Dim n1 As Double
    
    n1 = num
    
    t1 = Round(n1)
    
    If (n1 - t1 < 0) Then
        Floor = Round(n1 - 1)
    Else
        Floor = t1
    End If
    
    
    End Function

    Thanks

  7. #7
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    don't do that! an infinite loop will peg your cpu, making your application not responsive

    Because the interval of your timer is 100millisec, that is the resolution that you can expect. But please tell me, why do you need more precision than that? 100msec over 20minutes is a **** lot of resolution! Remember: if you are running NT (or 2K or XP) there is absoluteley no garantee that any timer will fire at the exact moment: you can achieve that only with a real time OS

    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  8. #8
    Join Date
    Mar 2005
    Posts
    49
    Thanks for your response Mstraf, I understand what you are trying to say. Thanks once again for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center