定时器跳过连接到下一台计算机,为每个循环使用RegistryKey.OpenRemoteBaseKey远程连接时
我构建了一个工具(使用Visual Studio 2015 Express - Visual Basic),它将从计算机的注册表中检查mcafee dat版本和日期可以手动输入,也可以输入文本文件或从活动目录中选择。该工具可以成功返回970台电脑/笔记本电脑中的714个信息。大多数故障都是因为它们无法在DNS中解析或不能ping通,并且工具可识别这些故障并成功记录它们。该工具花费了15分钟多一点时间来检索信息并将其记录在电子表格中。问题是,在失败的19,我得到了以下两个错误之一,这19把大部分的15分钟的工具和日志中获取的所有信息:定时器跳过连接到下一台计算机,为每个循环使用RegistryKey.OpenRemoteBaseKey远程连接时
试图执行未经授权的操作
-
网络路径找不到
是否有使用定时器,使得程序会尝试连接到注册表在这一点上... 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
您需要将您的请求放在另一个线程中。该线程可以被中止。
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
这个伟大的工程,但我确定它可以改进,所以如果你有它们,请回复建议。下面是代码:
尝试
昏暗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
你可以写一个循环而不是先等待。减少等待时间并循环数次。如果running = true,则退出循环。这会减少你的时间。 – pLurchi
我会努力这和时间都是双向的。我在这里添加了一个继续,并将其从6分半钟减少到3分半钟(如果它不能ping通,则转到下一台计算机,而不是运行其他2个任务)。
如果T20.Status = TaskStatus.Running然后
source1.Cancel()
持续
结束如果
我开始等待更改为一个循环,我记得需要花费大量时间才能成功检索远程信息并将其转换为Excel,而不会丢失Excel电子表格中的数据。例如,我将时间减少到了10毫秒,并且一些计算机没有足够快地响应ping,因此计算机和它的信息未被添加到电子表格中。同样,我减少了注册表任务中的ms,并且电子表格中缺少该计算机的注册表信息。
感谢您的回复。这应该是一个函数,以便它可以返回注册表值并更新电子表格? –
嗨,我认为你的代码很长一段时间是在第二次尝试声明。您可以在'endlessLoop'内复制这个内容,替换'您的代码'评论。然后调用'Main'而不是try语句。尝试的catch部分可以被复制以抓取endlessLoop的一部分。对不起格式不好,但通过智能手机回答。 – pLurchi
请操纵thrd.join语句。这是等待时间,直到请求以毫秒为单位中止。 – pLurchi