执行与否由代码验证! 75303.59-75294.38 = 9.21 耗时间9.21秒 Option Explicit Dim i As LongPrivate Sub Command1_Click() Debug.Print Timer Timer1.Enabled = True End SubPrivate Sub Form_Load() Timer1.Interval = 100 Timer1.Enabled = False End SubPrivate Sub Timer1_Timer() For i = 1 To 5000000 DoEvents Next Debug.Print Timer Timer1.Enabled = False End Sub
Private Sub Timer1_Timer() For i = 1 To 10000000 DoEvents Next Debug.Print Timer Timer1.Enabled = False End Sub耗时18.39秒
6楼使用MsgBox,将中断线程等待! Option Explicit Dim i As LongPrivate Sub Command1_Click() Debug.Print Timer Timer1.Enabled = True End SubPrivate Sub Form_Load() Timer1.Interval = 100 Timer1.Enabled = False End SubPrivate Sub Timer1_Timer() For i = 1 To 5000000 DoEvents Next MsgBox Timer Debug.Print Timer Timer1.Enabled = False End Sub
代码修改为: Option Explicit Dim i As LongPrivate Sub Command1_Click() Print Timer Timer1.Enabled = True End SubPrivate Sub Form_Load() Timer1.Interval = 100 Timer1.Enabled = False End SubPrivate Sub Timer1_Timer() For i = 1 To 5000000 DoEvents Next 'MsgBox Timer Print Timer Timer1.Enabled = False End Sub按PcTgl提示编译为EXE执行文件.执行结果如下:
Option Explicit Dim i As LongPrivate Sub Command1_Click() Print Timer Timer1.Enabled = True End SubPrivate Sub Form_Load() Timer1.Interval = 750 Timer1.Enabled = False End SubPrivate Sub Form_Unload(Cancel As Integer) End End SubPrivate Sub Timer1_Timer() For i = 1 To 5000000 DoEvents Next MsgBox Timer 'Print Timer 'Timer1.Enabled = False End Sub运行结果:试了3个周期,耗时33.03S
理解错了.代码: Option Explicit Dim i As LongPrivate Sub Command1_Click() Print Timer Timer1.Enabled = True End SubPrivate Sub Form_Load() Timer1.Interval = 100 Timer1.Enabled = False Timer2.Interval = 750 Timer2.Enabled = True End SubPrivate Sub Form_Unload(Cancel As Integer) End End SubPrivate Sub Timer1_Timer() For i = 1 To 5000000 DoEvents Next 'MsgBox Timer Print Timer 'Timer1.Enabled = False End SubPrivate Sub Timer2_Timer() Print "timer事件" End Sub我不太愿意使用msgbox,直接在窗体打印,效果:2个TIMER的计时周期不一:TIMER1约10S,TIMER2约25次/2个TIMER1代码周期.
MSDN: 当 Timer 事件获得控制后使运行的时间稍长于为 Interval 属性指定的时间。这将确保后台任务能够利用系统分配的每一时刻的处理器时间。直到这个事件结束处理之前,下一个 Timer 事件都在消息队列中等待。 上述内容好象是作为一个技术原则来描述的... 不过我对timer没研究,主观上一直也是这样认为的,刚才用1个timer加了10个label控件长时间填充caption,测试结果是顺序执行,编译后也一样。不知道方法对不对,代码:Option Explicit '一个timer,一个command,控件(数组)label1(0)一个,一个label2Private Declare Function GetTickCount Lib "kernel32" () As LongPrivate Function TimerTest(lab1 As Label, lab2 As Label) As Double '返回执行时间 Const NUM = 30000 Dim i As Long Dim t As Double
t = GetTickCount For i = 1 To NUM If i = 1 Or i = NUM Then lab1.Caption = i Else lab2.Caption = i End If DoEvents Next TimerTest = GetTickCount - t
End FunctionPrivate Sub Form_Load()
Dim i As Long
Timer1.Enabled = False Timer1.Interval = 1000
For i = 1 To 9 Load Label1(i) With Label1(i) .Top = Label1(i - 1).Top + 300 .Visible = True End With Next
End SubPrivate Sub Command1_Click() Timer1.Enabled = Not Timer1.Enabled End SubPrivate Sub Timer1_Timer() Static n As Long Print TimerTest(Label1(n), Label2) n = n + 1 If n = 10 Then Timer1.Enabled = False n = 0 End If End Sub
我觉得6楼的代码挺好的,删掉Form_Load事件的代码完全可以说明Timer时间是顺序执行的。 按照6楼的想法改写的 Private Sub Form_Load() Me.AutoRedraw = True Timer1.Interval = 1000 End SubPrivate Sub Timer1_Timer() Dim t As Date Print Now t = Now Do While DateDiff("s", t, Now) < 10 '耗费10秒时间 Loop End Sub
Option ExplicitPrivate Sub Form_Load()
Timer1_Timer
Dim a As Integer
a = 1
MsgBox aEnd SubPrivate Sub Timer1_Timer()MsgBox "b"End Sub实践证明 老马的答案是正确的
那个帖子里面有详细的讨论,也许lz很需要呢
75303.59-75294.38 = 9.21
耗时间9.21秒
Option Explicit
Dim i As LongPrivate Sub Command1_Click()
Debug.Print Timer
Timer1.Enabled = True
End SubPrivate Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = False
End SubPrivate Sub Timer1_Timer()
For i = 1 To 5000000
DoEvents
Next
Debug.Print Timer
Timer1.Enabled = False
End Sub
For i = 1 To 10000000
DoEvents
Next
Debug.Print Timer
Timer1.Enabled = False
End Sub耗时18.39秒
看看我贴的帖子地址,至少你应该好好看看再下结论,至于 6楼的代码,好像对vb还只是初级了解,连常识错误都犯了,还要拿出来打我一杆子
Option Explicit
Dim i As LongPrivate Sub Command1_Click()
Debug.Print Timer
Timer1.Enabled = True
End SubPrivate Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = False
End SubPrivate Sub Timer1_Timer()
For i = 1 To 5000000
DoEvents
Next
MsgBox Timer
Debug.Print Timer
Timer1.Enabled = False
End Sub
我本意是 用F8调试的 一步步执行 如果没有Timer1_Timer 这个调用 程序就无法暂停
我的意识是单步执行到a=1 看Timer1_Timer 会不会继续执行注: 我确实是新手 请多指教~
没办法
放个变量,记录上次运行时间
下次运行的时候判断一下
Option Explicit
Dim i As LongPrivate Sub Command1_Click()
Print Timer
Timer1.Enabled = True
End SubPrivate Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = False
End SubPrivate Sub Timer1_Timer()
For i = 1 To 5000000
DoEvents
Next
'MsgBox Timer
Print Timer
Timer1.Enabled = False
End Sub按PcTgl提示编译为EXE执行文件.执行结果如下:
Dim i As LongPrivate Sub Command1_Click()
Print Timer
Timer1.Enabled = True
End SubPrivate Sub Form_Load()
Timer1.Interval = 750
Timer1.Enabled = False
End SubPrivate Sub Form_Unload(Cancel As Integer)
End
End SubPrivate Sub Timer1_Timer()
For i = 1 To 5000000
DoEvents
Next
MsgBox Timer
'Print Timer
'Timer1.Enabled = False
End Sub运行结果:试了3个周期,耗时33.03S
Option Explicit
Dim i As LongPrivate Sub Command1_Click()
Print Timer
Timer1.Enabled = True
End SubPrivate Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = False
Timer2.Interval = 750
Timer2.Enabled = True
End SubPrivate Sub Form_Unload(Cancel As Integer)
End
End SubPrivate Sub Timer1_Timer()
For i = 1 To 5000000
DoEvents
Next
'MsgBox Timer
Print Timer
'Timer1.Enabled = False
End SubPrivate Sub Timer2_Timer()
Print "timer事件"
End Sub我不太愿意使用msgbox,直接在窗体打印,效果:2个TIMER的计时周期不一:TIMER1约10S,TIMER2约25次/2个TIMER1代码周期.
当 Timer 事件获得控制后使运行的时间稍长于为 Interval 属性指定的时间。这将确保后台任务能够利用系统分配的每一时刻的处理器时间。直到这个事件结束处理之前,下一个 Timer 事件都在消息队列中等待。
上述内容好象是作为一个技术原则来描述的...
不过我对timer没研究,主观上一直也是这样认为的,刚才用1个timer加了10个label控件长时间填充caption,测试结果是顺序执行,编译后也一样。不知道方法对不对,代码:Option Explicit
'一个timer,一个command,控件(数组)label1(0)一个,一个label2Private Declare Function GetTickCount Lib "kernel32" () As LongPrivate Function TimerTest(lab1 As Label, lab2 As Label) As Double
'返回执行时间
Const NUM = 30000
Dim i As Long
Dim t As Double
t = GetTickCount
For i = 1 To NUM
If i = 1 Or i = NUM Then
lab1.Caption = i
Else
lab2.Caption = i
End If
DoEvents
Next
TimerTest = GetTickCount - t
End FunctionPrivate Sub Form_Load()
Dim i As Long
Timer1.Enabled = False
Timer1.Interval = 1000
For i = 1 To 9
Load Label1(i)
With Label1(i)
.Top = Label1(i - 1).Top + 300
.Visible = True
End With
Next
End SubPrivate Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
End SubPrivate Sub Timer1_Timer()
Static n As Long
Print TimerTest(Label1(n), Label2)
n = n + 1
If n = 10 Then
Timer1.Enabled = False
n = 0
End If
End Sub
按照6楼的想法改写的
Private Sub Form_Load()
Me.AutoRedraw = True
Timer1.Interval = 1000
End SubPrivate Sub Timer1_Timer()
Dim t As Date
Print Now
t = Now
Do While DateDiff("s", t, Now) < 10 '耗费10秒时间
Loop
End Sub