问题是这样的:我的程序需要对的一个状态进行检测(由低变高),这个过程由一个function完成,如果在规定的时间内比如8sec中能够监测到状态的正常变化,那么我继续执行其他的程序,如果过了timerout的时间状态还是为低,则退出监测,报错。 我应该怎样用timer来完成这个程序?

解决方案 »

  1.   

    简单示例
    Dim int状态 As Long
    Dim intSecond As LongPrivate Sub Form_Load()
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End SubPrivate Sub Timer1_Timer()
        intSecond = intSecond + 1
        If intSecond > 6000 And return状态 < 10 Then
            Timer1.Enabled = False
            MsgBox "报错"
        ElseIf intSecond > 6000 And return状态 > 10 Then
            '正常执行完毕
            Timer1.Enabled = False
        End If
    End Sub
    Function return状态() As Long
        return状态 = int状态
    End Function
      

  2.   

    Private Sub Form_Load()
        Timer1.Interval = 1000
        Timer1.Enabled = false
    End SubPrivate Sub command1_click()  按下开始监测按钮
        Timer1.Enabled = True
        if functionA()=true then '设检测状态值的函数为functionA。
            如果开始即为高,是不是该进行一下异常处理。
        end if
    End SubPrivate Sub Timer1_Timer()
        Timer1.Enabled = false
        if functionA()=false then
            msgbox "出错"        
        end if
    end sub
      

  3.   

    但现在的问题是:我不想把timer放在form里面,我想把它放在一个module里?可以吗?
      

  4.   

    timer不能放到模块中
    你可以自己写个计数的函数