2016-07-27 45 views
0

我構建了一個工具(使用Visual Studio 2015 Express - Visual Basic),它將從計算機的註冊表中檢查mcafee dat版本和日期可以手動輸入,也可以輸入文本文件或從活動目錄中選擇。該工具可以成功返回970臺電腦/筆記本電腦中的714個信息。大多數故障都是因爲它們無法在DNS中解析或不能ping通,並且工具可識別這些故障併成功記錄它們。該工具花費了15分鐘多一點時間來檢索信息並將其記錄在電子表格中。問題是,在失敗的19,我得到了以下兩個錯誤之一,這19把大部分的15分鐘的工具和日誌中獲取的所有信息:定時器跳過連接到下一臺計算機,爲每個循環使用RegistryKey.OpenRemoteBaseKey遠程連接時

  1. 試圖執行未經授權的操作

  2. 網絡路徑找不到

    是否有使用定時器,使得程序會嘗試連接到註冊表在這一點上... RK1 = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine的方式, strComputer,RegistryView.Registry64),然後在一段時間後停止並移動到nex在每個循環中的計算機?我只在一年多的時間裏進行了編程,並且我只通過試驗/錯誤和谷歌學習,所以請耐心等待,因爲我不是一個經驗豐富的程序員。下面是代碼:

該項目工程以及我的目標在這裏是通過使跳到下一臺計算機時,它掛起的長時間來改善它。我篩選出了無法在DNS中解析或無法ping的計算機。

For Each sel In picker.SelectedObjects 
     Try 
     If HostIsResolvable(sel.Name) Then 
      Try 
       reply = ping.Send(sel.Name, 1) 
       If reply.Status = IPStatus.Success Then 
        IPAddr = reply.Address.ToString() 
        Try 
        comsys(sel.Name) 
        Dim rk1 As RegistryKey 
        Dim rk2 As RegistryKey 
        rk1 = RegistryKey.OpenRemoteBaseKey 
        (RegistryHive.LocalMachine, sel.Name, 
        RegistryView.Registry64) 
        rk2 = rk1.OpenSubKey 
        ("SOFTWARE\Wow6432Node\McAfee\AVEngine") 
        mAV = rk2.GetValue("AVDatVersion").ToString 
        mAD = rk2.GetValue("AVDatDate").ToString 
        objExcel.Cells(y, 1) = sel.Name 
        objExcel.Cells(y, 2) = IPAddr 
        objExcel.Cells(y, 3) = commodel 
        objExcel.Cells(y, 4) = comuser 
        objExcel.Cells(y, 5) = "DAT Version Number: " & mAV 
        objExcel.Cells(y, 6) = "DAT Date: " & mAD 
        y = y + 1 
        Catch ex As Exception 
        My.Computer.FileSystem.WriteAllText(Dell 
        & "\McAfeeDATeNumFailed.txt", sel.Name & "-Unable to 
        connect. Make sure this computer is on the network, 
        has remote administration enabled, and that both 
        computers are running the remote registry service. 
        Error message: " & ex.Message & vbCrLf, True) 
        End Try 
       Else 
        My.Computer.FileSystem.WriteAllText(Dell 
        & "\McAfeeDATeNumFailed.txt", sel.Name & " is not 
        pingable! " & vbCrLf, True) 
       End If 

      Catch ex As Exception 
        My.Computer.FileSystem.WriteAllText(Dell 
        & "\McAfeeDATeNumFailed.txt", sel.Name & "Ping error: 
        Unable to connect. Make sure this computer is on the 
        network, has remote administration enabled, and that 
        both computers are running the remote registry 
        service. Error message: " & ex.Message & vbCrLf, True) 
      End Try 
      Else 
      My.Computer.FileSystem.WriteAllText(Dell 
      & "\McAfeeDATeNumFailed.txt", sel.Name & " could not be 
      resolved in DNS! " & vbCrLf, True) 
      End If 
     Catch ex As Exception 
      My.Computer.FileSystem.WriteAllText(Dell 
      & "\McAfeeDATeNumFailed.txt", sel.Name & "DNS error: Unable to 
      connect. Make sure this computer is on the network, has remote 
      administration enabled, andd that both computers are running the 
      remote registry service. Error message: " & ex.Message & 
      vbCrLf, True) 
     End Try 
     sel = Nothing 
    Next 

回答

0

您需要將您的請求放在另一個線程中。該線程可以被中止。

Sub Main() 
    Dim thrd As New Thread(AddressOf endlessLoop) 'thread with your sub 
    thrd.Start() 'Start thread 
    thrd.Join(1000) 'Block until completion or timeout 

    If thrd.IsAlive Then 
     thrd.Abort() 'abort thread 
    Else 
     'thread finished already 
    End If 

End Sub 

Sub endlessLoop() 
    Try 
     While True 
      'Your Code 
     End While 
    Catch ex As ThreadAbortException 
     'Your code when thread is killed 
    End Try 
End Sub 

希望這會有所幫助。

「*****編輯*** 您的代碼看起來是這樣的(如果有任何變量,子傳我沒有選中)

For Each sel In picker.SelectedObjects 
    Try 
     If HostIsResolvable(sel.Name) Then 
      Try 
       reply = ping.Send(sel.Name, 1) 
       If reply.Status = IPStatus.Success Then 
        IPAddr = reply.Address.ToString() 
        call timerThread 'New 
       Else 
        My.Computer.FileSystem.WriteAllText(Dell 
        & "\McAfeeDATeNumFailed.txt", sel.Name & " is not 
        pingable! " & vbCrLf, True) 
       End If 

      Catch ex As Exception 
       My.Computer.FileSystem.WriteAllText(Dell 
       & "\McAfeeDATeNumFailed.txt", sel.Name & "Ping error: 
       Unable to connect. Make sure this computer is on the 
       network, has remote administration enabled, and that 
       both computers are running the remote registry 
       service. Error message: " & ex.Message & vbCrLf, True) 
      End Try 
     Else 
     My.Computer.FileSystem.WriteAllText(Dell 
     & "\McAfeeDATeNumFailed.txt", sel.Name & " could not be 
     resolved in DNS! " & vbCrLf, True) 
     End If 
    Catch ex As Exception 
     My.Computer.FileSystem.WriteAllText(Dell 
     & "\McAfeeDATeNumFailed.txt", sel.Name & "DNS error: Unable to 
     connect. Make sure this computer is on the network, has remote 
     administration enabled, andd that both computers are running the 
     remote registry service. Error message: " & ex.Message & 
     vbCrLf, True) 
    End Try 
    sel = Nothing 
Next 



Sub timerThread() 
    Dim thrd As New Thread(AddressOf registryRequest) 'thread with your sub 
    thrd.Start() 'Start thread 
    thrd.Join(15000) 'Block until completion or timeout (15 seconds) 

    If thrd.IsAlive Then 
     thrd.Abort() 'abort thread 
    Else 
     'thread finished already 
    End If 
End Sub 

Sub registryRequest() 
    Try 
     comsys(sel.Name) 
     Dim rk1 As RegistryKey 
     Dim rk2 As RegistryKey 
     rk1 = RegistryKey.OpenRemoteBaseKey 
     (RegistryHive.LocalMachine, sel.Name, 
     RegistryView.Registry64) 
     rk2 = rk1.OpenSubKey 
     ("SOFTWARE\Wow6432Node\McAfee\AVEngine") 
     mAV = rk2.GetValue("AVDatVersion").ToString 
     mAD = rk2.GetValue("AVDatDate").ToString 
     objExcel.Cells(y, 1) = sel.Name 
     objExcel.Cells(y, 2) = IPAddr 
     objExcel.Cells(y, 3) = commodel 
     objExcel.Cells(y, 4) = comuser 
     objExcel.Cells(y, 5) = "DAT Version Number: " & mAV 
     objExcel.Cells(y, 6) = "DAT Date: " & mAD 
     y = y + 1 
    Catch ex As ThreadAbortException 
     My.Computer.FileSystem.WriteAllText(Dell 
     & "\McAfeeDATeNumFailed.txt", sel.Name & "-Unable to 
     connect. Make sure this computer is on the network, 
     has remote administration enabled, and that both 
     computers are running the remote registry service. 
     Error message: " & ex.Message & vbCrLf, True) 
    End Try 
End Sub 
+0

感謝您的回覆。這應該是一個函數,以便它可以返回註冊表值並更新電子表格? –

+0

嗨,我認爲你的代碼很長一段時間是在第二次嘗試聲明。您可以在'endlessLoop'內複製這個內容,替換'您的代碼'評論。然後調用'Main'而不是try語句。嘗試的catch部分可以被複制以抓取endlessLoop的一部分。對不起格式不好,但通過智能手機回答。 – pLurchi

+0

請操縱thrd.join語句。這是等待時間,直到請求以毫秒爲單位中止。 – pLurchi

0

這個偉大的工程,但我確定它可以改進,所以如果你有它們,請回復建議。下面是代碼:

嘗試

昏暗source1中作爲新CancellationTokenSource

昏暗令牌作爲的CancellationToken = source1.Token

昏暗T20作爲任務= Task.Factory.StartNew(功能()getping ((sel.Name),令牌))

T20.Wait(30)

如果T20.Status = TaskStatus。運行然後

source1.Cancel() 

My.Computer.FileSystem.WriteAllText(Dell & "\McAfeeDATeNumFailed.txt", sel.Name & " Ping timed out. The task was disposed of at " & ex_time & "." & vbCrLf & vbCrLf, True) 

結束如果

昏暗源2作爲新CancellationTokenSource

昏暗token2作爲的CancellationToken = source2.Token

昏暗T21作爲任務= Task.Factory.StartNew(功能() comsys((sel.Name),token2))

T21.Wait(500)

如果T21.Status = TaskStatus.Running然後

source2.Cancel() 

My.Computer.FileSystem.WriteAllText(Dell & "\McAfeeDATeNumFailed.txt", sel.Name & " RPC error. The task was disposed of at " & ex_time & "." & vbCrLf & vbCrLf, True) 

結束如果

昏暗source3作爲新CancellationTokenSource

昏暗token3作爲的CancellationToken = source3.Token

昏暗T22作爲任務=任務.Factory.StartNew(Function()getregvalues((sel.Name),token3))

T22.Wait(600)

如果T22.Status = TaskStatus.Running然後

source3.Cancel() 

My.Computer.FileSystem.WriteAllText(Dell & "\McAfeeDATeNumFailed.txt", sel.Name & " Error retrieving registry value. The task was disposed of at " & ex_time & "." & vbCrLf & vbCrLf, True) 

結束如果

IPADDR = reply.Address.ToString()

objExcel.Cells(Y,1)= SEL。命名

objExcel.Cells(Y,2)= IPADDR

objExcel.Cells(Y,3)=便桶

objExcel.Cells(Y,4)= comuser

objExcel.Cells(Y,5)= 「DAT版本號:」 & MAV

objExcel.Cells(Y,6)=「DAT日期:「& MAD

Y = Y + 1

IPADDR =無

答覆=無

commodel =無

comuser =無

SEL =無

了Thread.Sleep(10)

抓住EX作爲例外

結束Try

+0

你可以寫一個循環而不是先等待。減少等待時間並循環數次。如果running = true,則退出循環。這會減少你的時間。 – pLurchi

0

我會努力這和時間都是雙向的。我在這裏添加了一個繼續,並將其從6分半鐘減少到3分半鐘(如果它不能ping通,則轉到下一臺計算機,而不是運行其他2個任務)。

如果T20.Status = TaskStatus.Running然後

source1.Cancel()

持續

結束如果

0

我開始等待更改爲一個循環,我記得需要花費大量時間才能成功檢索遠程信息並將其轉換爲Excel,而不會丟失Excel電子表格中的數據。例如,我將時間減少到了10毫秒,並且一些計算機沒有足夠快地響應ping,因此計算機和它的信息未被添加到電子表格中。同樣,我減少了註冊表任務中的ms,並且電子表格中缺少該計算機的註冊表信息。

相關問題