|
-
Mar 17th, 2005, 05:44 PM
#1
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
-
Mar 17th, 2005, 06:58 PM
#2
Hyperactive Member
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
-
Mar 17th, 2005, 08:04 PM
#3
Re: Why are .NET timers slow, even on 1ms interval???
VB Code:
Private mfrmNew As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mfrmNew.Show()
Timer2.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
'This is a normal timer
mfrmNew.Top = mfrmNew.Top - 1
End Sub
Private Sub Timer2_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer2.Elapsed
'This is a component timer
mfrmNew.Top = mfrmNew.Top - 1
End Sub
Hope that helps.
Woka
-
Mar 17th, 2005, 08:43 PM
#4
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.
-
Mar 17th, 2005, 11:42 PM
#5
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:
Imports System
Imports System.Runtime.InteropServices
Namespace Org.Mentalis.Utilities
Public NotInheritable Class StopWatch
Private m_StartTime As Long
Private m_Frequency As Long
Private Declare Ansi Function QueryPerformanceCounter Lib "kernel32.dll" (ByRef x As Long) As Integer
Private Declare Ansi Function QueryPerformanceFrequency Lib "kernel32.dll" (ByRef x As Long) As Integer
Public Sub New()
Frequency = GetFrequency()
Reset()
End Sub
Public Sub Reset()
StartTime = GetValue()
End Sub
Public Function Peek() As Long
Return CType((((GetValue() - StartTime) / CType(Frequency, Double)) * 10000), Long)
End Function
Private Function GetValue() As Long
Dim ret As Long = 0
If QueryPerformanceCounter(ret) = 0 Then Throw New NotSupportedException("Error while querying the high-resolution performance counter.")
Return ret
End Function
Private Function GetFrequency() As Long
Dim ret As Long = 0
If QueryPerformanceFrequency(ret) = 0 Then Throw New NotSupportedException("Error while querying the performance counter frequency.")
Return ret
End Function
Private Property StartTime() As Long
Get
Return m_StartTime
End Get
Set(ByVal Value As Long)
m_StartTime = Value
End Set
End Property
Private Property Frequency() As Long
Get
Return m_Frequency
End Get
Set(ByVal Value As Long)
m_Frequency = Value
End Set
End Property
End Class
End Namespace
-
Mar 18th, 2005, 12:48 AM
#6
New Member
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:
Private Sub timerTicker()
While True
stopwatch.Reset()
While (stopwatch.Peek / CType(10, Single) < _interval)
Thread.Sleep(_sleepInt)
End While
RaiseEvent Tick()
End While
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|