tagCANDY CGI VBレスキュー(花ちゃん) の Visual Basic 2010 用 掲示板(VB.NET 掲示板) [ツリー表示へ]   [Home]
一括表示(VB.NET VB2005)
タイトルQueueTimerでタイマー処理
記事No11849
投稿日: 2017/06/03(Sat) 04:35
投稿者年金生活
timeSetEventでのタイマー処理は危険をはらんでるとの指摘を受けましたので、
QueueTimerを使ってタイマー処理するように書いてみました。
今回も一回捨てコマンドを挟みました、それでも立ちあがりは若干ブレています。
立ち上がりはスキップして使おうと考えています。
バグ落ちするような部分がありましたら、ご指摘ください。
Win7/VB2013、VB歴ほぼなし。

1mSec: 0.00 1.07 1.68 0.07 1.02 0.97 0.96 0.99 1.04 0.99 0.99 0.96 1.00 0.93 1.06 0.92 0.99 0.94
5mSec: 0.00 5.04 6.14 3.70 4.97 4.98 4.96 4.98 4.99 4.96 5.01 4.98 4.94 4.98 4.98 4.99 4.96 4.98
10mSec: 0.00 10.05 11.88 7.95 9.96 9.98 10.00 9.93 9.97 10.32 9.63 9.97 9.96 10.01 10.04 9.92 10.01 9.90
50mSec: 0.00 50.13 50.51 49.30 49.97 49.97 49.97 49.97 49.97 49.97 50.01 49.92 49.98 49.95 49.94 49.97 49.98 49.95

=============================================
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        QueTimerStart(50)
    End Sub
End Class
---------------------------------------------
Module Module1
    Public Declare Function CreateTimerQueue Lib "kernel32.dll" () As Integer
    Public Declare Function CreateTimerQueueTimer Lib "kernel32.dll" ( _
        ByRef phNewTimer As Integer, _
        ByVal TimerQueue As Integer, _
        ByVal Callback As WaitOrTimerCallback, _
        ByVal Parameter As Integer, _
        ByVal DueTime As Integer, _
        ByVal Period As Integer, _
        ByVal Flags As Integer) As Integer
    Public Declare Function DeleteTimerQueue Lib "kernel32.dll" ( _
        ByVal TimerQueue As Integer) As Integer
    Public Declare Function DeleteTimerQueueTimer Lib "kernel32.dll" ( _
        ByVal TimerQueue As Integer, _
        ByVal Timer As Integer, _
        ByVal CompletionEvent As Integer) 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 Delegate Sub WaitOrTimerCallback(state As Object, timedOut As Boolean)
    Public tcb As WaitOrTimerCallback
    Public hQueue, hTimer As Long

    ' <<<< タイマの起動 >>>>
    Public Sub QueTimerStart(ByVal mSec As Integer)
        Call timeBeginPeriod(1)
        hQueue = CreateTimerQueue()
        hTimer = 0
        tcb = AddressOf TimerCallBack

        Call CreateTimerQueueTimer(hTimer, hQueue, tcb, 0, 0, 0, 0)
        System.Threading.Thread.Sleep(100)
        sw.Restart()
        Call CreateTimerQueueTimer(hTimer, hQueue, tcb, 0, mSec, mSec, 0)
    End Sub

    ' <<<< タイマの停止 >>>>
    Public Sub QueTimerStop()
        Call DeleteTimerQueueTimer(hQueue, hTimer, 0)
        Call DeleteTimerQueue(hQueue)
        Call timeEndPeriod(1)
        System.Threading.Thread.Sleep(100)
    End Sub

    ' <<<< 高精度タイマで繰り返す >>>>
    Public Sub TimerCallBack()
        iCount += 1
        If iCount > 10 Then
            ' <<<< 繰り返す部分 >>>>
        End If

        sw.Stop()
        Console.Write((sw.Elapsed.ToString("fffff") / 100).ToString("0.00 "))

        sw.Restart()
        If iCount > 17 Then QueTimerStop()
    End Sub
End Module

[ツリー表示へ]