タイトル | : timeSetEventでタイマー処理 |
記事No | : 11842 |
投稿日 | : 2017/05/31(Wed) 06:24 |
投稿者 | : 年金生活 |
既存のtimerは分解能も精度も悪いので、timeSetEventで書いてみました。 1〜50mSecで時間を計測してみた結果は以下のようになりました。 立ち上がり時がうまくいっていません。(実際は25mSec程度で使うので、実用上はオケ) 改善する方法はありますか? バグ落ちしそうなところはありませんか? ちなみにWin7/VB2013です、VB歴ほぼなし。
1mSec: 2.57 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.85 0.99 0.90 0.99 0.98 0.98 0.98 0.98 0.99 0.98 5mSec: 5.97 0.00 3.38 4.99 5.02 4.98 4.91 5.00 5.02 5.01 4.94 4.99 4.97 4.94 5.01 5.02 4.93 5.01 10mSec: 11.73 2.52 10.00 9.97 9.95 9.99 9.96 9.98 9.98 9.99 9.96 9.95 10.00 9.98 9.98 9.99 9.97 50mSec: 51.91 42.45 49.98 49.94 49.97 49.97 49.93 49.94 49.98 49.91 49.98 49.95 50.01 49.98 49.90
========================================= Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click sw.Start() Call TimerStart(25) End Sub End Class =========================================== Module Module1
Public Delegate Sub TimerProcDelegate(ByVal uID As Integer, _ ByVal uMsg As Integer, _ ByVal dwUser As Integer, _ ByVal dw1 As Integer, _ ByVal dw2 As Integer) Public Declare Function timeBeginPeriod Lib "winmm.dll" ( _ ByVal uPeriod As Integer) As Integer Public Declare Function timeEndPeriod Lib "winmm.dll" ( _ ByVal uPeriod As Integer) As Integer Public Declare Function timeSetEvent Lib "winmm.dll" ( _ ByVal uDelay As Integer, _ ByVal uResolution As Integer, _ ByVal lpFunction As TimerProcDelegate, _ ByVal dwUser As Integer, _ ByVal uFlags As Integer) As Integer Public Declare Function timeKillEvent Lib "winmm.dll" ( _ ByVal uID As Integer) As Integer
Public gIntTimerID As Integer '高精度タイマID
Public sw As New System.Diagnostics.Stopwatch() Private proc As TimerProcDelegate
' <<<< タイマの起動 >>>> Public Sub TimerStart(ByVal mSec As Integer) Call timeBeginPeriod(1) proc = AddressOf TimerProc gIntTimerID = timeSetEvent(mSec, 1, proc, 0, 1) End Sub
' <<<< タイマの停止 >>>> Public Sub TimerStop() Call timeKillEvent(gIntTimerID) Call timeEndPeriod(50) System.Threading.Thread.Sleep(100) End Sub
' <<<< 高精度タイマで繰り返す >>>> Public Sub TimerProc(ByVal uTimerID As Integer, ByVal uMsg As Integer, ByVal dwUser As Integer, ByVal dw1 As Integer, ByVal dw2 As Integer)
sw.Stop() Console.Write((sw.Elapsed.ToString("fffff") / 100).ToString("0.00") & " ") sw.Restart()
End Sub End Module
|