单线程程序,需要对串口读写,发现在调试状态下可以成功,我估计可能是延时的问题请问在vb可有类似于delay,sleep之类的等待函数,可以令程序延迟一段时间继续执行

解决方案 »

  1.   

    Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
      

  2.   

    Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
      

  3.   

    sleep好像會全個程序都停起來,有可能會發生錯誤
    delay要自組Public Function Delay(Mins%, Secs%, Optional ByRef StopFlag) As Long
    Dim EndOfDelay
    EndOfDelay = DateAdd("n", Mins, Now)
    EndOfDelay = DateAdd("s", Secs, EndOfDelay)
    Delay = 0
    Do While (Now < EndOfDelay)
    DoEvents
    If Not IsMissing(StopFlag) Then
    If StopFlag Then
    Delay = 1
    StopFlag = False
    Exit Do
    End If
    End If
    Loop
    End Function
    使用例子:Dim StopTheTimer As BooleanPrivate Sub Command1_Click() '開始延時
    Dim lRetval&
    lRetval = Delay(1, 5, StopTheTimer)
    If lRetval = 0 Then
    MsgBox "時間到!"
    Else
    MsgBox "取消延時!"
    End If
    End Sub
    Private Sub Command2_Click() '取消延時
    StopTheTimer = True
    End Sub
      

  4.   

    Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)调用方法:Sleep(millionSecond)还有也可以引入一个空循环来延迟。

    dim i as Long
    for i=1 to 100000        
    next i
      

  5.   

    Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
      

  6.   

    我是这么做的,在模块里
    Declare Function GetTickCount Lib "kernel32" () As Long '获得系统自开机经过的毫秒数
    Sub delay(ii As Integer)    '时间延迟
        Dim tt As Long
        tt = GetTickCount()
        Do
            DoEvents
        Loop Until GetTickCount - tt > ii
    End Sub
    在用的时候只要,DELAY(XXX)   XXX为毫秒
      

  7.   

    Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
      

  8.   

    虽然我也不知道是什么函数,但是我觉得应该有吧!实在不行我有如下建议,简单明了:用一个时钟控件,通过控制它的Enable=True或者False来达到目的。在你合适的地方使始终True,在时钟运行代码中的最后写上Enable=False不一样能够达到你的目的吗?
      

  9.   

    '延时函数
    'Call PauseProgram(0.5)   '0.5是延时0.5秒,你可以设其它值
    Public Sub PauseProgram(ByVal PauseTime As Single)
        Dim snlTime As Single
        snlTime = Timer
        DoEvents
        Do Until Timer - snlTime > PauseTime
        Loop
    End Sub我一直用的,成功的。^_^
      

  10.   

    我是这样来延时的Dim currSecond  As Integer
                '没有需要要下载的。则延时3秒            
                currSecond = Second(Time)
                Do While (Second(Time) - currSecond) < 3
                    '下面这条语句,是防止currSecond在>=57时出现的Second(Time)-currSecond永远不会>=3的情况。
                    If (currSecond - Second(Time)) > 0 Then If (currSecond - Second(Time)) <= 57 Then Exit Do
                    DoEvents
                Loop
      

  11.   

    小建,你的 DoEvents应该放在do loop 里面吧
      

  12.   

    多谢各位兄弟。
    我用sleep成功了,但是确实是令整个程序都被中止,这一点比较麻烦。下午揭帖
      

  13.   

    sleep的确有点恶心,不过要看你什么时候用
      

  14.   

    自己写函数,原则:
    1 不用sleep
    2 不用DoEvents,用了DoEvents可能会产生不可预知的错误
      

  15.   

    Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
      

  16.   


      Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)  Call Sleep(1000)
      

  17.   

    Dim Start As Long   
        Start = Timer
         Do While Timer < Start + 3    ' 3 秒的延时
            DoEvents  '转让控制权
         Loop