循环一个函数? [英] Loop a function?
问题描述
是否有可能循环一个函数,直到一个item = TRUE?
我试图ping一个服务器...
连接建立后,或Ping = TRUE,那么程序将执行。
如果连接没有建立,ping就会重复,直到它为TRUE。
到目前为止,我的代码如下所示。
如果为TRUE,MyProgram将打开。
如果为False,则会再次调用该函数。但是,这不会发生......实际上没有发生,它只是退出。
任何帮助很乐意。
如果有人知道完成此任务的更有效方法,请告诉我。
谢谢!
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ GetObject(winmgmts:{impersonationLevel = impersonate})。ExecQuery(select * from Win32_PingStatus where address ='&strHost&')
For Each oRetStatus In oPing
如果IsNull(oRetStatus.StatusCode)或oRetStatus.StatusCode<> 0然后
bReturn = False
Else
bReturn = True
End If
Set oRetStatus = Nothing
Next
Set oPing = Nothing
Ping = b返佣
结束功能
如果Ping然后
呼叫MyProgram
其他
呼叫PingSub
End If
Sub MyProgram
Dim objShell
Set objShell = WScript.CreateObject(WScript.Shell)
objShell.Run(\\\\\\\\\' \\ MyProgram.exe)
Set objShell = Nothing
End Sub
Sub PingSub
Call Ping
End Sub
试试像这样:
Option Explicit
Dim MyLoop,strComputer,objPing,objStatus
MyLoop = True
MyLoop = True
strComputer =smtp。 gmail.com
Set objPing = GetObject(winmgmts:{impersonationLevel = impersonate}!\ \)。ExecQuery _
(select * from Win32_PingStatus where address ='& strComputer& ')
对于objPing中的每个objStatus
如果objStatus.Statuscode = 0则
MyLoop = False
调用MyProgram()
wscript.quit
结束如果
下一个
暂停(10)'睡10个秒
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
结束Sub
'************************************* ************************************************** *******
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. If connection is not established, then the ping will repeat until it is TRUE.
My code thus far is below. If TRUE, MyProgram will open. If False, the function will be called again. But this does not occur...actually nothing occurs, it just exits.
Any help is gladly appreciated. If anyone knows a more efficient way of completing this task, then please let me know. Thank you!
Function Ping
Dim oPing, oRetStatus, bReturn
Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address='" & "strHost" & "'")
For Each oRetStatus In oPing
If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
bReturn = False
Else
bReturn = True
End If
Set oRetStatus = Nothing
Next
Set oPing = Nothing
Ping = bReturn
End Function
If Ping Then
Call MyProgram
Else
Call PingSub
End If
Sub MyProgram
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("\\Path\To\My\Program.exe")
Set objShell = Nothing
End Sub
Sub PingSub
Call Ping
End Sub
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
'**********************************************************************************************
这篇关于循环一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!