2012-02-24 84 views
1

我有低於該代碼工作正常,通過行步驟查驗每個主機,並更新表。VBA自主對象

Sub Do_ping() 
    Set output = ActiveWorkbook.Worksheets(1) 
    With ActiveWorkbook.Worksheets(1) 
    Set pinger = CreateObject("WScript.Shell") 

    pings = 1 
    pingend = "FALSE" 

output.Cells(2, 4) = pings 
output.Cells(2, 5) = pingend 

    Do 

    Row = 2 

    Do 
    If .Cells(Row, 1) <> "" Then 
    result = pinger.Run("%comspec% /c ping.exe -n 1 -w 250 " _ 
     & output.Cells(Row, 1).Value & " | find ""TTL="" > nul 2>&1", 0, True) 

    If (result = 0) = True Then 
    result = "TRUE" 
    Else 
    result = "FALSE" 
    End If 

    ' result = IsConnectible(.Cells(Row, 1), 1, 1000) 
    output.Cells(Row, 2) = result   

    End If 
    Row = Row + 1 
    Loop Until .Cells(Row, 1) = "" 

    waitTime = 1 
    Start = Timer 
    While Timer < Start + waitTime 
    DoEvents 
    Wend 


output.Cells(2, 4) = pings 
output.Cells(2, 5) = pingend 

pings = pings + 1 

Loop Until pingend = "TRUE" 

    End With 



End Sub 

但假設我有50個設備,其中40個設備已關閉。因爲它是順序的,所以我必須等待ping在這些設備上超時,因此一次傳遞可能需要很長時間。

我在VBA可以創建一個對象,我可以創造的,每個ping一臺獨立的主機乘實例,然後簡單的循環,雖然對象拉回從他們真/假財產。

我不知道該怎麼可能,這是或你如何應對VBA類。

我要像

set newhostping = newobject(pinger) 
pinger.hostname = x.x.x.x 

一些事情來設置對象,然後對象將有邏輯

do 
ping host x.x.x.x 
if success then outcome = TRUE 
if not success then outcome = FALSE 
wait 1 second 
loop 

所以回到主代碼,我可以只使用

x = pinger.outcome 

給我主機的當前狀態,而不需要等待當前的ping操作完成。這將只返回最後完成的嘗試

沒有任何一個有他們可以分享任何代碼或想法的結果呢?

謝謝

DevilWAH

+0

您是否考慮過正確縮進代碼以使其他人更容易閱讀和理解? – 2012-02-25 20:36:12

回答

1

您可以使用ShellAndWait以下功能異步運行這些調用(即平行)。使用簡單的tracert命令查看我的示例,通常需要幾秒鐘才能運行。它會打開50個同時運行的命令窗口。

Option Explicit 

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _ 
      ByVal dwProcessId As Long) As Long 

Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long 

Private Const STATUS_PENDING = &H103& 
Private Const PROCESS_QUERY_INFORMATION = &H400 

Public Sub test() 

    Dim i As Long 
    For i = 1 To 50 
     ShellandWait "tracert www.google.com", vbNormalFocus, 1 
    Next i 

End Sub 

Public Function ShellandWait(parProgramName As String, Optional parWindowStyle As VbAppWinStyle = vbMinimizedNoFocus, _ 
      Optional parTimeOutValue As Long = 0) As Boolean 
'source: http://www.freevbcode.com/ShowCode.Asp?ID=99 
'Time out value in seconds 
'Returns true if the program closes before timeout 

    Dim lInst As Long 
    Dim lStart As Long 
    Dim lTimeToQuit As Long 
    Dim sExeName As String 
    Dim lProcessId As Long 
    Dim lExitCode As Long 
    Dim bPastMidnight As Boolean 

    On Error GoTo ErrorHandler 

    lStart = CLng(Timer) 
    sExeName = parProgramName 

    'Deal with timeout being reset at Midnight 
    If parTimeOutValue > 0 Then 
    If lStart + parTimeOutValue < 86400 Then 
     lTimeToQuit = lStart + parTimeOutValue 
    Else 
     lTimeToQuit = (lStart - 86400) + parTimeOutValue 
     bPastMidnight = True 
    End If 
    End If 

    lInst = Shell(sExeName, parWindowStyle) 

    lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst) 

    Do 
    Call GetExitCodeProcess(lProcessId, lExitCode) 
    DoEvents 
    If parTimeOutValue And Timer > lTimeToQuit Then 
     If bPastMidnight Then 
     If Timer < lStart Then Exit Do 
     Else 
     Exit Do 
     End If 
    End If 
    Loop While lExitCode = STATUS_PENDING 

    If lExitCode = STATUS_PENDING Then 
    ShellandWait = False 
    Else 
    ShellandWait = True 
    End If 
    Exit Function 

ErrorHandler: 
    ShellandWait = False 

End Function 
+0

你是對的這是一種方式,但是這仍然會使一些人的問題比別人花費更長的時間。假設我有10臺主機可以ping,並且我想每10秒鐘ping一次。但我想使用20秒的時間(對於我知道的ping來說,確實是愚蠢的值),我不想讓其他設備等待超時。所以就像我說我正在創建一個自動ping主機並擁有關於該主機的各種值(%成功,最後一次失敗,總成功,總失敗,當前狀態......)的對象。 – DevilWAH 2012-02-25 14:53:57

+0

我還沒有試過,但如果說9個主機返回ping和1次了,你會得到9真從shellandwait和1個假晚了一點。沒有什麼能夠阻止你在等待第10場的時候重新獲得第9場。現在如果你想正確的並行和獨立處理它是不是真的由VBA支持,你或許應該考慮另一種語言(不知道VB,C#/ Java的definutely,使用方便發出shell命令)... – assylias 2012-02-25 20:39:43

+0

你可以同時檢查這(我還沒有測試過):http://stackoverflow.com/a/8181329/829571 – assylias 2012-02-25 20:42:40