Knight_Chester_Sun 2015-06-13 07:35 采纳率: 0%
浏览 5588

如何在线程上使用timer来做一个定时器 in VB.NET

代码如下。思路是这样的:在线程中使用threading.timer,1ms执行一次,使GLO_TICK++。Calculate_Interval 用来计算间隔时间。
实际上TimerTask不是1ms执行一次,这是为什么?

 Module Module1
    Class StateObjClass
        ' Used to hold parameters for calls to TimerTask 
        Public SomeValue As Integer
        Public TimerReference As System.Threading.Timer
        Public TimerCanceled As Boolean
        Public GLO_TICK As Long
    End Class

    Public StateObj As New StateObjClass
    Public long_Temp1 As Long
    Public int16_temp1 As Int16

    Sub RunTimer()

        StateObj.TimerCanceled = False
        StateObj.SomeValue = 1
        Dim TimerDelegate As New Threading.TimerCallback(AddressOf TimerTask)
        ' Create a timer that calls a procedure every 2 seconds. 
        ' Note: There is no Start method; the timer starts running as soon as  
        ' the instance is created. 
        Dim TimerItem As New System.Threading.Timer(TimerDelegate, StateObj, _
                                                   0, 1)
        StateObj.TimerReference = TimerItem  ' Save a reference for Dispose.
        Dim a As Boolean = False

        Dim state As Int16
        While True ' Run for ten loops.
            'System.Threading.Thread.Sleep(1000)  ' Wait one second.

            Select Case state
                Case 0
                    If a Then
                        long_Temp1 = StateObj.GLO_TICK
                    Else
                        a = True
                        long_Temp1 = StateObj.GLO_TICK
                        Console.WriteLine("a=" & a & Now)
                    End If
                    state = 1
                    Exit Select
                Case 1
                    Calculate_Interval(long_Temp1, int16_temp1)
                    If int16_temp1 > 100 Then
                        a = False
                        long_Temp1 = StateObj.GLO_TICK
                        Console.WriteLine("a=" & a & Now)
                        state = 2
                    End If
                    Exit Select
                Case 2
                    Calculate_Interval(long_Temp1, int16_temp1)
                    If int16_temp1 > 100 Then
                        a = True
                        long_Temp1 = StateObj.GLO_TICK
                        Console.WriteLine("a=" & a & Now)
                        state = 1
                    End If
                    Exit Select
            End Select

        End While

        StateObj.TimerCanceled = True  ' Request Dispose of the timer object.
    End Sub

    Sub TimerTask(ByVal StateObj As Object)
        Dim State As StateObjClass = CType(StateObj, StateObjClass)

        If State.TimerCanceled Then    ' Dispose Requested.
            System.Diagnostics.Debug.WriteLine("Done  " & Now)
            State.TimerReference.Dispose()
        End If

        System.Threading.Interlocked.Increment(State.GLO_TICK)
    End Sub
    Sub Main()
        RunTimer()
        Console.Read()

    End Sub

    Function Calculate_Interval(ByVal IntervalStart As Long, ByRef Interval_ms As Int16) As Int16
        Interval_ms = Convert.ToInt16(StateObj.GLO_TICK - IntervalStart)
        Return Interval_ms
    End Function

End Module

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-06-13 08:41
    关注

    没有定时器能保证1ms的定时精度。你非要这么做,得死循环。

    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页