有语段代码
for each a in b
...
...
next
现在想控制循环体的执行时间,比如每1秒或者0.1秒运行一次,应该如何控制比较好呢。谢谢了。

解决方案 »

  1.   

    我想用timer实现,可想了半天也没想到非常好的方式,能帮忙想个方案么?
    用sleep等等的延时方式,对系统开销比较大,并且时间控制不准确,不希望使用。
      

  2.   


    你加doevents就可以解决了
      

  3.   

    假如我希望1分钟循环一次的话,需要多少个sleep延时啊,另外对不同配置的机器延时时间也不一样
      

  4.   

    本帖最后由 bcrun 于 2010-12-23 14:12:04 编辑
      

  5.   

    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 
      

  6.   

    更正一下:
    Dim t As Longt = Timer + 1For Each a in b
    If Timer > t Then Exit For
    ......
    NextDo Until Timer > t
    Loop 
      

  7.   


    sleep是有参数的,比如sleep(1000),参数是时间的,貌似和机器没有关系
      

  8.   

    没听说sleep有什么系统开销。如果楼主对时间误差的要求不是特别高(允许误差10ms左右),还是很容易实现。
      

  9.   

    那你的需求陈述全都理解错了。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
      

  10.   

    呵呵,可能是理解上真的有偏差了,我希望是循环体部分定时,也就是说针对b当中的每一个a,相隔一定时间执行一次。
    Private Sub Go_Loop()
    For Each a in b
    ......
    Next
    End SubPrivate Sub Timer1_OnTimer()
    Go_Loop
    End Sub
    这个样子就是每隔一定时间,对整个循环都做一次了,不符合我的要求。
    原始需求是这样,b是文件夹下的图片,我想遍历这些图片,然后在屏幕上显示,但需要控制时间。
      

  11.   

    让你搞复杂了,你把所有文件读入一个列表(如果你不想显示这个列表,隐藏就可以了),然后用timer显示就可以了
      

  12.   

    Private Sub Timer1_Timer()
        On Error GoTo errT
        Randomize
        Picture1.Picture = LoadPicture(List1.List((Rnd() * List1.ListCount)))
    errT:End Sub
      

  13.   

    这个是随机的,lz不要随机的修改一下就可以了Private Sub Command1_Click()
        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
      

  14.   

    你把文件夹下的文件都读入数组,然后启动timer不就可以了
      

  15.   

    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
      

  16.   


    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
      

  17.   

    可以Sleep(1)和DoEvents结合使用,以降低CPU占用率。
      

  18.   

    一分钟循环一次的话,用TIMER控件就可以了.
    设置TIMER的触发时间为60000,六万毫秒(60秒),然后在TIMER的触发事件中遍历一次控件.
    如果遍历一次的对象数量比较大的话可以在FOR EACH循环中加上DO EVENTS来保持程序响应