📄 990503a.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="KJ Wang">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<title>Timer 的问题及解决方案?</title>
</head>
<body bgcolor="#FFFFFF" link="#FF0000" vlink="#000080"
alink="#FF0000">
<h2 align="center"><font color="#FF0000">新概念的 Visual
Basic 6.0 教程 - </font><font color="#FF0000" size="5"><b>补充教材</b></font></h2>
<p><a name="1"></a></p>
<hr>
<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td><h3>Q:</h3>
</td>
<td><h3>我想在 4:30 PM 自动执行一些程序,
因此利用 Timer 控件的特性, 在 Timer 事件程序中判断
Time = #04:30:00 PM#, 结果到了 4:30 PM 的时候, 程序却未被执行?<font color="#0000FF" size="4">(05/03)</font></h3>
</td>
</tr>
</table>
<hr>
<blockquote>
<p align="left">由于 Timer 有延迟发生的现象,
所以当我们用 Timer 来计时时, 要注意 Timer
事件发生的时间可能会不太准确。 假设我们所设置的 Timer 时间间隔是 1 秒钟,
并且预期 04:29:59 PM 之后发生 Timer
事件的时间是 04:30:00 PM, 那么极可能因为 Timer
的延迟而使得真正发生的时间变成 04:30:01 PM 或更后面的时间, 所以 Time = #04:30:00 PM#
比较运算式就不会成立, 如果改成:『Time
>= #04:30:00 PM#』, 则即使 Timer
略有延迟,依然可以正常执行程序。 </p>
</blockquote>
<p><a name="2"></a></p>
<hr>
<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td><h3>Q:</h3>
</td>
<td><h3>我想用 Timer 做长时间的定时, 例如 5
分钟, 但 Interval 属性值最大只能设置到
65535(大约只有 65 秒), 所以想在 Timer
事件程序中宣告一个 counter 变数以累计计时,
来达成 5 分钟计时的目的, 是否可行? <font
color="#0000FF">(05/03)</font></h3>
</td>
</tr>
</table>
<hr>
<blockquote>
<p>由于 Timer 有延迟的现象,
所以累计计时将造成更严重的延迟,
要比较准确地计时不能完全仰赖 Timer
事件的功能, 以下建议的作法是将 Interval 属性设置为 1000(= 1 秒),
然后在 Timer 事件程序中读取『时间差』来判断是否已经届满
5 分钟, 程序大致如下:</p>
<blockquote>
<pre><font color="#0000FF">Private Sub Timer1_Timer()
Static pre_time As Date ' 5 分钟的起算点
' 第一次要设置系统时间给 pre_time
If pre_time = 0 Then pre_time = Now
' 从『起算时间』到『现在』是否超过 5 分钟
If DateDiff("n", pre_time, Now) >= 5 Then
MsgBox "Do something" ' 执行您要做的事情
pre_time = Now ' 重新起算
End If
End Sub</font></pre>
</blockquote>
</blockquote>
<blockquote>
<p>以上程序中的 DateDiff 函数是以 (Now - pre_time)
来计算时间差,而计算的单位取决于参数一的
"n"(表示分数),所以:</p>
<blockquote>
<pre><font color="#0000FF">DateDiff( "n", pre_time, Now)</font></pre>
</blockquote>
<p>等于 Now 与 pre_time 的分数差。</p>
</blockquote>
<hr>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -