我在程序中用的vb的timer控件,间隔是1s,每一秒会有事情处理,到第10s时,进行数据插入,可是发现数据中的时间间隔不对,有的是10s,有的是11s还有的是14s、8s等等。我也知道vb的定时器控件不准。我试验了一个程序 timer控件就给一个文本框数值加1的动作 运行了大概300s的时候就跟系统时间差错了1s。不知道要怎么解决 现在有2个问题 1:怎样让定时更准确 2:定时器中断的服务中是不是不能有太多代码。小弟一直坐单片机,对vb的语法不太熟悉。在单片机中可以在main函数加个查询,这样在定时器中就置一个标志变量就可以,可是在vb中如何实现????急急急,在线等,解决马上送分,谢谢~~~~~~~~~~~~~
option explicit type large_integer
lowpart as long
highpart as long
end type public declare function queryperformancecounter lib "kernel32" _
(lpperformancecount as large_integer) as long
public declare function queryperformancefrequency lib "kernel32" _
(lpfrequency as large_integer) as long
public declare function timesetevent lib "winmm.dll" (byval udelay as long, byval _
uresolution as long, byval lpfunction as long, byval dwuser as long, _
byval uflags as long) as long
public declare function timekillevent lib "winmm.dll" (byval uid as long) as long
public declare function gettickcount lib "kernel32" () as long public lmsfreq as long
public timercount as single
public lmmcount as single
public ltimeid as long
public acttime1 as long
public acttime2 as long
public icountstart as single dim icount as single '注释: timesetevent的回调函数
sub timeproc(byval uid as long, byval umsg as long, byval dwuser as long, _
byval dw1 as long, byval dw2 as long)
form1.text2.text = format$(lmmcount, "00.00")
lmmcount = lmmcount - 0.01
if lmmcount <= 0 then
icountstart = 60
lmmcount = 60
timercount = 60
endcount
end if
end sub
sub endcount()
icount = icountstart
icountstart = 0
timekillevent ltimeid
acttime2 = gettickcount - acttime1
with form1
.command1.enabled = true
.command2.enabled = false
.timer1.enabled = false
.text1 = "计数器记时" + format$((60 - icount), "00.00") + " " _
+ "实际经过时间" + format$((acttime2 / 1000), "00.00")
.text2 = "计数器记时" + format$((60 - lmmcount), "00.00") + " " _
+ "实际经过时间" + format$((acttime2 / 1000), "00.00")
.text3 = "计数器记时" + format$((60 - timercount), "00.00") + " " _
+ "实际经过时间" + format$((acttime2 / 1000), "00.00")
end with
end sub
其实用GetTickCount这个函数自己写也很简单啊,这个函数返回Windows启动后到目前为止所经过的时间,以微秒为单位。把GetTickCount放在Do ... Loop中,加个DoEvents,几句代码就搞定了。