.Net CF下精确的计时器
用法:
Dim t as New AtomicCF.Timer
t.start()
....'Some functions here
Dim TimeLapsed as Long = t.stop()
Imports
System.Runtime.InteropServices
Namespace AtomicCF Namespace AtomicCF
Public Class TimerClass Timer
<DllImport("coredll.dll", EntryPoint:="QueryPerformanceCounter")> _
Public Shared Function QueryPerformanceCounter()Function QueryPerformanceCounter(ByRef perfCounter As Long) As Integer
End Function
<DllImport("coredll.dll", EntryPoint:="QueryPerformanceFrequency")> _
Public Shared Function QueryPerformanceFrequency()Function QueryPerformanceFrequency(ByRef frequency As Long) As Integer
End Function
Private m_frequency As Int64
Private m_start As Int64
Public Sub New()Sub New()
If QueryPerformanceFrequency(m_frequency) = 0 Then
Throw New ApplicationException
End If
'Convert to ms.
m_frequency = CLng(m_frequency / 1000)
End Sub
Public Sub Start()Sub Start()
If QueryPerformanceCounter(m_start) = 0 Then
Throw New ApplicationException
End If
End Sub
Public Function [()Function [Stop]() As Int64 Dim lStop As Int64 = 0 If QueryPerformanceCounter(lStop) = 0 Then Throw New ApplicationException End If Return CLng((lStop - m_start) / m_frequency) End Function End ClassEnd Namespace Namespace
Namespace AtomicCF Namespace AtomicCF
Public Class TimerClass Timer
<DllImport("coredll.dll", EntryPoint:="QueryPerformanceCounter")> _
Public Shared Function QueryPerformanceCounter()Function QueryPerformanceCounter(ByRef perfCounter As Long) As Integer
End Function
<DllImport("coredll.dll", EntryPoint:="QueryPerformanceFrequency")> _
Public Shared Function QueryPerformanceFrequency()Function QueryPerformanceFrequency(ByRef frequency As Long) As Integer
End Function
Private m_frequency As Int64
Private m_start As Int64
Public Sub New()Sub New()
If QueryPerformanceFrequency(m_frequency) = 0 Then
Throw New ApplicationException
End If
'Convert to ms.
m_frequency = CLng(m_frequency / 1000)
End Sub
Public Sub Start()Sub Start()
If QueryPerformanceCounter(m_start) = 0 Then
Throw New ApplicationException
End If
End Sub
Public Function [()Function [Stop]() As Int64 Dim lStop As Int64 = 0 If QueryPerformanceCounter(lStop) = 0 Then Throw New ApplicationException End If Return CLng((lStop - m_start) / m_frequency) End Function End ClassEnd Namespace Namespace