如题

解决方案 »

  1.   

    '*******************************************************
    '函数名称:delay
    '参数:N
    '返回值:
    '功能:产生延迟,延迟时间为N
    '*******************************************************
    Private Sub Delay(ByVal n As Single)
        Dim tm1 As Single, tm2 As Single
        tm1 = Timer
        Do
            tm2 = Timer
            If tm2 < tm1 Then tm2 = tm2 + 86400
            If tm2 - tm1 > n Then Exit Do
            DoEvents
        Loop
    End Sub
      

  2.   

    或者使用API函数:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)程序中用
    sleep 500'延时半秒
    就可以了
      

  3.   

    Public Declare Function timeGetTime Lib "winmm.dll" () As Long
    Public Sub delay(i As Integer)
        Dim t1 As Long
        Dim t2 As Long
        t1 = timeGetTime
        While (t2 - t1 < i * 1000)   //*1000,那i就是要延时i秒,级别可以自己定
            t2 = timeGetTime
            DoEvents
        Wend
    End Sub
      

  4.   

    用Sleep,那么你的程序就不能相应其他的控制了。直到延迟的时间过去。
      

  5.   

    '延时程序
    Public Sub DelayTime(ByVal nSecond As Integer)
        Dim EndTime As Date
        EndTime = DateAdd("s", nSecond, Time)
        While Time < EndTime
            DoEvents
        Wend
    End Sub
      

  6.   

    用sleep()
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    括号里面的数字代表微秒
    sleep(1000)就是延时1秒
      

  7.   

    我有一个:
    Function GetCurTime() As Currency
        If mInit = False Then
            If QueryPerformanceFrequency(mFrequency) = 0 Then
                mFrequency = 0
            End If
            mInit = True
        End If
        
        If mFrequency <> 0 Then
            Dim CurCount As Currency
            Call QueryPerformanceCounter(CurCount)
            GetCurTime = CurCount * 1000@ / mFrequency
            'Debug.Print GetCurTime()
        Else
            GetCurTime = GetTickCount()
        End If
        
    End FunctionSub Delay(ByVal n As Currency)
    Dim Timer1 As Currency, Timer2 As Currency
    Timer1 = GetCurTime()
    Do
    Timer2 = GetCurTime()
    If Timer2 - Timer1 > n Then Exit Do
    DoEvents
    Loop
    End Sub
      

  8.   

    一般情况下用dreamtou 的就可以,在有些情况下用我的这个比较好些。
      

  9.   

    就这些啊?
    无非是利用Doevents啊!
      

  10.   

    以上的两种延时方法各有弊端
    1.使用sleep的方法在期间你就做不了任何操作
    2.使用循环的方法则在循环期间会占用百分百的CPU.
    当然有第三种方式,能克服以上的缺点.不过它是一个DLL来的,有兴趣要的话发邮件给我[email protected]
      

  11.   

    TO DragonSchool(龙堂) :
     1.使用sleep的方法在期间你就做不了任何操作
    这点我不敢苟同.
    你的话有一定的道理,比如延时一秒钟用sleep(1000)的话确实在这一秒钟内什么也干不了
    但是如果这样:
    dim i
     for i=0 to 100 step 1
       sleep(10)
       doevents
    next i 
    这样的话不就解决了吗
      

  12.   

    在c语言中,不能用delay延迟函数,和cpu有关。应该用截获windows时钟中断的方法得到准确的时间延迟。
      

  13.   

    Public Function Delay(ms As Long)
    Do Until i > Val(100 * ms)
    DoEvents
    i = i + 1
    Loop
    End Function
      

  14.   

    做长时间延迟建议用gettickcount(要引入的API)
    dim begin as long
    begin = gettickcount'延迟if gettickcount - begin > 5000 then ......         '看当前是否比上次运行gettickcount时过了5秒或许可以用now取时间,然后用datediff()判断时间差以上2法都可以延迟长时间,建议不要用sleep,因为它是让你整个进程sleep掉
      

  15.   

    补充:用上面2种办法做的延迟就完全看你的机器的时间了,相对来说不收CPU影响(只是相对),因为它们的条件都是机器的当前时间,用timer的话延迟时间有限制(timer的interval是Long,好象只能到360000),而上面说的2种办法可以不受时间长短限制
    用法:可以放在timer中,timer触发一次就判断一次时间间隔,这个时候,timer的interval越小,延迟越精确
      

  16.   

    使用API函数:
    Private Declare Function SetTimer Lib "user32" Alias "SetTimer" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    SetTimer 0,0,1000,Addressof 在模块中的函数或过程
      

  17.   

    使用API函数:
    Private Declare Function KillTimer Lib "user32" Alias "KillTimer" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long结束时钟。