Dim t As Longt1 = TimerFor Each a in b If Timer - t > 1000 Then Exit For ‘这样不就跳出了for循环了么?我是希望循环体每隔一定时间执行一次,b中所有的a都要遍历到。 ...... DoEvents NextDo Until Timer - t > 1000 DoEvents Loop
更正一下: Dim t As Longt = Timer + 1For Each a in b If Timer > t Then Exit For ...... NextDo Until Timer > t Loop
那你的需求陈述全都理解错了。1 你必须确认循环在 1 秒内完成。2 要用 Timer 定时。否则你的程序就是一个超占用资源的死循环。Private Sub Go_Loop() For Each a in b ...... Next End SubPrivate Sub Timer1_OnTimer() Go_Loop End SubPrivate Command1() Timer1.Interval = 1000 Timer1.Enabled = True End Sub
呵呵,可能是理解上真的有偏差了,我希望是循环体部分定时,也就是说针对b当中的每一个a,相隔一定时间执行一次。 Private Sub Go_Loop() For Each a in b ...... Next End SubPrivate Sub Timer1_OnTimer() Go_Loop End Sub 这个样子就是每隔一定时间,对整个循环都做一次了,不符合我的要求。 原始需求是这样,b是文件夹下的图片,我想遍历这些图片,然后在屏幕上显示,但需要控制时间。
subDisplayPicture End SubPrivate Sub Drive1_Change() Dir1.Path = Drive1.Drive
End SubSub sosuofile(MyPath As String) Dim Myname As String Dim dir_i() As String Dim i, idir As Long
If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\" Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While Myname <> "" If Myname <> "." And Myname <> ".." Then If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录 idir = idir + 1 ReDim Preserve dir_i(idir) As String dir_i(idir - 1) = Myname Else List1.AddItem "" & MyPath & Myname '把找到的文件显示到列表框中 End If End If Myname = Dir '搜索下一项 Loop
For i = 0 To idir - 1 Call sosuofile(MyPath + dir_i(i)) Next i
ReDim dir_i(0) As String
End SubPrivate Sub Form_Load() Command1.Caption = "搜索" Timer1.Enabled = False Timer1.Interval = 300
End SubPrivate Sub subDisplayPicture() Timer1.Enabled = True
End SubPrivate Sub Timer1_Timer() On Error GoTo errT Randomize Picture1.Picture = LoadPicture(List1.List((Rnd() * List1.ListCount))) errT:End Sub
你把文件夹下的文件都读入数组,然后启动timer不就可以了
Public Function Wait(ByVal millSec As Double) As Long Dim startTime As Double startTime = Timer millSec = millSec / 1000 While Timer - startTime < millSec DoEvents WendEnd Function
Private Sub Command1_Click() Dim a As Variant, b() As Byte b = StrConv("abcdef", vbFromUnicode)
For Each a In b Debug.Print a ys 1 NextEnd SubPrivate Sub ys(t As Single) Dim t1 As Single t1 = Timer While Timer - t1 <= t DoEvents Wend End Sub
用sleep等等的延时方式,对系统开销比较大,并且时间控制不准确,不希望使用。
你加doevents就可以解决了
If Timer - t > 1000 Then Exit For ‘这样不就跳出了for循环了么?我是希望循环体每隔一定时间执行一次,b中所有的a都要遍历到。
......
DoEvents
NextDo Until Timer - t > 1000
DoEvents
Loop
Dim t As Longt = Timer + 1For Each a in b
If Timer > t Then Exit For
......
NextDo Until Timer > t
Loop
sleep是有参数的,比如sleep(1000),参数是时间的,貌似和机器没有关系
For Each a in b
......
Next
End SubPrivate Sub Timer1_OnTimer()
Go_Loop
End SubPrivate Command1()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Go_Loop()
For Each a in b
......
Next
End SubPrivate Sub Timer1_OnTimer()
Go_Loop
End Sub
这个样子就是每隔一定时间,对整个循环都做一次了,不符合我的要求。
原始需求是这样,b是文件夹下的图片,我想遍历这些图片,然后在屏幕上显示,但需要控制时间。
On Error GoTo errT
Randomize
Picture1.Picture = LoadPicture(List1.List((Rnd() * List1.ListCount)))
errT:End Sub
List1.Clear
sosuofile (Dir1.List(Dir1.ListIndex))
MsgBox "搜索完毕!,共找到" + Str(List1.ListCount) + "条记录。", vbOKOnly + vbExclamation, "提示"
subDisplayPicture
End SubPrivate Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End SubSub sosuofile(MyPath As String)
Dim Myname As String
Dim dir_i() As String
Dim i, idir As Long
If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While Myname <> ""
If Myname <> "." And Myname <> ".." Then
If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
idir = idir + 1
ReDim Preserve dir_i(idir) As String
dir_i(idir - 1) = Myname
Else
List1.AddItem "" & MyPath & Myname '把找到的文件显示到列表框中
End If
End If
Myname = Dir '搜索下一项
Loop
For i = 0 To idir - 1
Call sosuofile(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String
End SubPrivate Sub Form_Load()
Command1.Caption = "搜索"
Timer1.Enabled = False
Timer1.Interval = 300
End SubPrivate Sub subDisplayPicture()
Timer1.Enabled = True
End SubPrivate Sub Timer1_Timer()
On Error GoTo errT
Randomize
Picture1.Picture = LoadPicture(List1.List((Rnd() * List1.ListCount)))
errT:End Sub
Dim startTime As Double
startTime = Timer
millSec = millSec / 1000 While Timer - startTime < millSec DoEvents WendEnd Function
Private Sub Command1_Click()
Dim a As Variant, b() As Byte
b = StrConv("abcdef", vbFromUnicode)
For Each a In b
Debug.Print a
ys 1
NextEnd SubPrivate Sub ys(t As Single)
Dim t1 As Single
t1 = Timer
While Timer - t1 <= t
DoEvents
Wend
End Sub
设置TIMER的触发时间为60000,六万毫秒(60秒),然后在TIMER的触发事件中遍历一次控件.
如果遍历一次的对象数量比较大的话可以在FOR EACH循环中加上DO EVENTS来保持程序响应