System.Timers.Timer的(.NET)很慢? [英] System.Timers.Timer (.NET) really slow?
问题描述
我下了System.Timers.Timer的创建自己的线程的IM pression和微软建议使用此类型的计时器来做到这一点做更精确的时序(相对于Windows.Forms.Timer任务,它运行在UI线程)。
I was under the impression that System.Timers.Timer creates its own thread and that Microsoft recommends this type of timer to do tasks that do more accurate timing (as opposed to Windows.Forms.Timer, which runs in the UI thread).
下面(我认为)的code应该是复制和pasteable到项目与空的形式。在我的机器,我不能让tmrWork打勾任何超过每秒约60倍的速度,这是令人惊讶的稳定。
The code below (I think) should be copy-and-pasteable into a project with an empty form. On my machine, I cannot get tmrWork to tick any faster than about 60 times per second, and it's amazingly unstable.
Public Class Form1
Private lblRate As New Windows.Forms.Label
Private WithEvents tmrUI As New Windows.Forms.Timer
Private WithEvents tmrWork As New System.Timers.Timer
Public Sub New()
Me.Controls.Add(lblRate)
InitializeComponent()
End Sub
Private StartTime As DateTime = DateTime.Now
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
Handles MyBase.Load
tmrUI.Interval = 100
tmrUI.Enabled = True
tmrWork.Interval = 1
tmrWork.Enabled = True
End Sub
Private Counter As Integer = 0
Private Sub tmrUI_Tick(sender As Object, e As System.EventArgs) _
Handles tmrUI.Tick
Dim Secs As Integer = (DateTime.Now - StartTime).TotalSeconds
If Secs > 0 Then lblRate.Text = (Counter / Secs).ToString("#,##0.0")
End Sub
Private Sub tmrWork_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) _
Handles tmrWork.Elapsed
Counter += 1
End Sub
End Class
在这个特定的简单的情况下,把一切在tmrUI将产生相同的性能。我想我从来没有试图让一个System.Timers.Timer的走的太快,但是这样的表现看起来太糟糕了我。我写我自己的类使用高性能定时器硬件,但好像应该有一个内置的定时器,可以做,说每秒100滴答?
In this particular simple case, putting everything in tmrUI will yield the same performance. I guess I never tried to get a System.Timers.Timer to go too fast, but this performance looks just too bad to me. I wrote my own class to use the high performance timer in hardware but it seems like there should be a built-in timer that can do, say 100 ticks per second?
这是怎么回事吗?
推荐答案
要获得接近100Hz的尝试是这样的,它使用的的AutoResetEvent。
To get near 100Hz try something like this which uses an AutoResetEvent.
Private Sub tmrWork()
'start this as a background thread
Dim tmr As New Threading.AutoResetEvent(False)
Dim stpw As Stopwatch = Stopwatch.StartNew
Const interval As Integer = 10 'the interval
'
'
Do 'timer
stpw.Stop()
If stpw.ElapsedMilliseconds > interval Then
tmr.WaitOne(interval) 'the interval
Else
tmr.WaitOne(CInt(interval - stpw.ElapsedMilliseconds)) 'the interval
End If
stpw.Restart()
'
'code to execute when 'timer' elapses
'
Loop
End Sub
下面是一个测试表明,这取决于你在回路中做的,就可以关火code在100Hz。
Here is a test that shows that, depending on what you do in the loop, it is possible to fire off code at 100Hz.
Private Sub tmrWork()
Dim tmr As New Threading.AutoResetEvent(False)
Dim stpw As Stopwatch = Stopwatch.StartNew
Const interval As Integer = 10 'the interval
'for testing
Dim cts As New List(Of Long)
'
'
Do 'timer
tmr.WaitOne(interval) 'wait for the interval
cts.Add(stpw.ElapsedMilliseconds) 'add elapsed to list
stpw.Restart() 'restart
If cts.Count >= 500 Then 'five second test
Debug.WriteLine(String.Format("Avg: {0}, Min: {1}, Max: {2}", cts.Average, cts.Min, cts.Max))
Stop
End If
Loop
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim t As New Threading.Thread(AddressOf tmrWork)
t.IsBackground = True
t.Start()
End Sub
有关汉斯Pissant
For Hans Pissant
平台计时器分辨率:平台计时器分辨率默认的平台计时器分辨率是15.6ms(15625000ns),并应尽量使用该系统处于空闲状态。如果计时器分辨率增加,处理器的电源管理技术未必有效。定时器的分辨率可能由于多媒体播放或图形动画增加。当前计时器分辨率(100ns的单位)100000最大计时器周期(100ns的单位)156001
Platform Timer Resolution:Platform Timer Resolution The default platform timer resolution is 15.6ms (15625000ns) and should be used whenever the system is idle. If the timer resolution is increased, processor power management technologies may not be effective. The timer resolution may be increased due to multimedia playback or graphical animations. Current Timer Resolution (100ns units) 100000 Maximum Timer Period (100ns units) 156001
平台计时器分辨率:出色的计时器请求程序或服务请求的计时器分辨率小于平台最大计时器分辨率。请求期间的100000请求进程ID:452请求进程路径\设备\ HarddiskVolume3 \ Windows \ System32下\ svchost.exe的
Platform Timer Resolution:Outstanding Timer Request A program or service has requested a timer resolution smaller than the platform maximum timer resolution. Requested Period 100000 Requesting Process ID 452 Requesting Process Path \Device\HarddiskVolume3\Windows\System32\svchost.exe
这篇关于System.Timers.Timer的(.NET)很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!