Results 1 to 6 of 6

Thread: Why are .NET timers slow, even on 1ms interval???

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Why are .NET timers slow, even on 1ms interval???

    In VB6 if I have a timer to move a form:
    Code:
    mfrmForm.Left = mfrmForm.Left + 1
    ...and I set the interval of the timer to 1, then the form flys off the screen. VERY fast.

    If I do the same in .NET then it moves slowly...not THAT slowly...but slower than a form in VB6 would if the timer was set to 30ms!

    I have used standard timer, and the component timer, but this has not made any difference.

    Any ideas?

    Woka

  2. #2
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Why are .NET timers slow, even on 1ms interval???

    Could you show me all the code for this? I would like to try it.

    Jim

  3. #3

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Why are .NET timers slow, even on 1ms interval???

    VB Code:
    1. Private mfrmNew As New Form2
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         mfrmNew.Show()
    5.         Timer2.Enabled = True
    6.     End Sub
    7.  
    8.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    9.         'This is a normal timer
    10.         mfrmNew.Top = mfrmNew.Top - 1
    11.     End Sub
    12.  
    13.     Private Sub Timer2_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer2.Elapsed
    14.         'This is a component timer
    15.         mfrmNew.Top = mfrmNew.Top - 1
    16.     End Sub
    Hope that helps.

    Woka

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Why are .NET timers slow, even on 1ms interval???

    Never use Timers, especially in .NET. Timers are horribly slow, inaccurate, inconsistant, and not real time. Use managed loops such as Do Loops, only with frame limiting API's, such as QueryPerformanceCounter, QueryPerformanceFrequency, GetTickCount, etc. If done correctly, it'll run in real time.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Why are .NET timers slow, even on 1ms interval???

    This is a class used during one of the early Forum Contests. Try it:

    VB Code:
    1. Imports System
    2. Imports System.Runtime.InteropServices
    3.  
    4. Namespace Org.Mentalis.Utilities
    5.    
    6.     Public NotInheritable Class StopWatch
    7.         Private m_StartTime As Long
    8.         Private m_Frequency As Long
    9.         Private Declare Ansi Function QueryPerformanceCounter Lib "kernel32.dll" (ByRef x As Long) As Integer
    10.          Private Declare Ansi Function QueryPerformanceFrequency Lib "kernel32.dll" (ByRef x As Long) As Integer
    11.          Public Sub New()
    12.             Frequency = GetFrequency()
    13.             Reset()
    14.         End Sub
    15.        Public Sub Reset()
    16.             StartTime = GetValue()
    17.         End Sub
    18.         Public Function Peek() As Long
    19.             Return CType((((GetValue() - StartTime) / CType(Frequency, Double)) * 10000), Long)
    20.         End Function
    21.         Private Function GetValue() As Long
    22.             Dim ret As Long = 0
    23.             If QueryPerformanceCounter(ret) = 0 Then Throw New NotSupportedException("Error while querying the high-resolution performance counter.")
    24.             Return ret
    25.         End Function
    26.         Private Function GetFrequency() As Long
    27.             Dim ret As Long = 0
    28.             If QueryPerformanceFrequency(ret) = 0 Then Throw New NotSupportedException("Error while querying the performance counter frequency.")
    29.             Return ret
    30.         End Function
    31.          Private Property StartTime() As Long
    32.             Get
    33.                 Return m_StartTime
    34.             End Get
    35.             Set(ByVal Value As Long)
    36.                 m_StartTime = Value
    37.             End Set
    38.         End Property
    39.         Private Property Frequency() As Long
    40.             Get
    41.                 Return m_Frequency
    42.             End Get
    43.             Set(ByVal Value As Long)
    44.                 m_Frequency = Value
    45.             End Set
    46.         End Property
    47.        
    48.     End Class
    49. End Namespace

  6. #6
    New Member
    Join Date
    Mar 2005
    Posts
    15

    Re: Why are .NET timers slow, even on 1ms interval???

    I am having a similar problem. I have come up with somewhat of a fix by writing my own timer class which runs in its own thread, using the stopwatch class referred to above. The thread runs in this sub:

    VB Code:
    1. Private Sub timerTicker()
    2.         While True
    3.             stopwatch.Reset()
    4.             While (stopwatch.Peek / CType(10, Single) < _interval)
    5.                 Thread.Sleep(_sleepInt)
    6.             End While
    7.             RaiseEvent Tick()
    8.         End While
    9.     End Sub

    The problem with this code is, even if _sleepInt is set to 1, the actual delay is 15.4ms or so. If sleepInt is 0, the timer is very accurate, but then the cpu usage is 100%. Both are unacceptable. Is there any other way besides thread.sleep to take a burden off the cpu which won't compromise my interval accuracy?

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