Loop a function?

前端 未结 2 542
离开以前
离开以前 2020-12-12 03:33

Is it possible to loop a function until an item = TRUE?

I am trying to ping a server... Once connection is established, or Ping = TRUE, then a program will execute.

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 04:19

    Try like this :

    Option Explicit
    Dim MyLoop,strComputer,objPing,objStatus
    MyLoop = True
    While MyLoop = True
        strComputer = "smtp.gmail.com"
        Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery _
        ("select * from Win32_PingStatus where address = '" & strComputer & "'")
        For Each objStatus in objPing
            If objStatus.Statuscode = 0 Then
                MyLoop = False
                Call MyProgram()
                wscript.quit
            End If
        Next
        Pause(10) 'To sleep for 10 secondes
    Wend
    '**********************************************************************************************
     Sub Pause(NSeconds)
        Wscript.Sleep(NSeconds*1000)
     End Sub
    '**********************************************************************************************
    Sub MyProgram()
    Dim objShell
    Set objShell = CreateObject( "WScript.Shell" )
    objShell.Run("calc.exe")
    Set objShell = Nothing
    End Sub
    '**********************************************************************************************
    

提交回复
热议问题