码迷,mamicode.com
首页 > 其他好文 > 详细

VB实现毫秒计时方案

时间:2014-12-19 00:30:55      阅读:1169      评论:0      收藏:0      [点我收藏+]

标签:des   http   ar   io   os   使用   sp   for   on   

VB实现毫秒计时方案

GetTickCount是最简单的获取时间的API函数,返回值是从开机到现在的时间,单位为毫秒。虽然它的返回值单位是毫秒,但是精度并不是1毫秒,误差大约在55毫秒左右,MSDN中这样说“If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.”。

multimedia timer:
timeGetTime函数实现的效果与GetTickCount差不多,返回值也是从开机到现在的时间,单位为毫秒。但是它的精度仍然不能达到1毫秒,误差在5毫秒以上,MSDN中这样说“Windows NT/2000: The default precision of the timeGetTime function can be five milliseconds or more, depending on the machine. ”。

high-resolution timer:
计算机里所能达到的最高精度就是使用QueryPerformanceFrequency和QueryPerformanceCounter这两个API,这两个API都是获取硬件(比如说CPU)的计数器,不同的硬件获取的值也不同。
其中QueryPerformanceFrequency是获取振荡频率,每秒振荡多少次;QueryPerformanceCounter是获取当前振荡次数。
例如:
假设QueryPerformanceFrequency获取的振荡频率是每秒5000次,应用程序第一次调用QueryPerformanceCounter获得的振荡次数是1500次,第二次调用QueryPerformanceCounter获得的振荡次数是3500次,那么这两次调用QueryPerformanceCounter的时间间隔就是(3500-1500)/5000=0.04秒

 

如果仅仅为了计算时间偏差,可以使用 GetSystemTimeAsFileTime,这个精度可以达到100纳秒,

msdn有个介绍。

http://msdn.microsoft.com/ZH-CN/library/windows/desktop/ms724284(v=vs.85).aspx

Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

 

It is not recommended that you add and subtract values from the FILETIME structure to obtain relative times. Instead, you should copy the low- and high-order parts of the file time to a ULARGE_INTEGER structure, perform 64-bit arithmetic on the QuadPart member, and copy the LowPart and HighPart members into the FILETIME structure.

Do not cast a pointer to a FILETIME structure to either a ULARGE_INTEGER* or __int64* value because it can cause alignment faults on 64-bit Windows.

 

 

方案1:使用GetLocalTime

Private Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
Private Type SYSTEMTIME       
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
End Type
Private Sub Form_Load()
Timer1.Interval = 1
End Sub
Private Sub Timer1_Timer()
Dim t As SYSTEMTIME
GetLocalTime t
Cls
Print DateSerial(t.wYear, t.wMonth, t.wDay) & " " & TimeSerial(t.wHour, t.wMinute, t.wSecond) & "." & t.wMilliseconds
End Sub

 

 

方案2:

GetTickCount是最简单的获取时间的API函数,返回值是从开机到现在的时间,单位为毫秒。虽然它的返回值单位是毫秒,但是精度并不是1毫秒,误差大约在55毫秒左右,MSDN中这样说“If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.”。

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Function My_Delay(sSeconds As Long) ‘延时函数 Delay(毫秒)
Begin = GetTickCount
a = 0
Do While a < sSeconds
a = GetTickCount - Begin
DoEvents
Loop
End Function

Private Sub Command1_Click()
My_Delay 1000
MsgBox "123"
End Sub

 

方案3:

Private Sub Command1_Click()
dim c,d,e

c = Timer
d = Timer
e = d - c

msgbox “时间差:” & e
End Sub

VB实现毫秒计时方案

标签:des   http   ar   io   os   使用   sp   for   on   

原文地址:http://www.cnblogs.com/dhaichen/p/4172873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!