态不正常则触发另一线程得到该IP的信息
Sub GetStausF()
Dim ServiceP As New ServiceController() ‘实例化一个ServiceController类
ServiceP.MachineName = IMachineIP
ServiceP.ServiceName = IServiceName
Dim myRow As DataRow
Dim status As String
Dim Run As Boolean = False
myRow = ds.Tables(ITable).NewRow
Try
If ServiceP.Status.ToString <> "Running" Then
status = ServiceP.Status.ToString‘如果状态不是RUNNING则将状态赋予字符串变量
Else
Run = True ‘如果状态为RUNNING,则不做任何事
End If
Catch er As Exception ‘以下处理取得状态时候发生的异常
status = Left(er.Message, 35)
If InStr(status, "Service Control Manager") = 0 Then
status = "Not installed or open service failed" ‘没有安装该服务
ElseIf InStr(er.Message, "Manager") > 0 Then
status = "Can not detected" ‘服务的状态不可得
End If
End Try
ServiceP.Close() ‘关闭ServiceController实例
‘以下判断如果状态不是RUNNING,则记录该系统,并触发线程得到它的详细信息。
If Not Run Then
myRow("msg") = status
myRow("ip") = IMachineIP
SyncLock GetType(AddRow) ‘为保证多线程情况下,对DataSet只有一个写操作,锁定AddRow类
Dim AddRowIns As New AddRow(myRow) ‘将IP和状态通过我们自己写的AddRow类插入DataSet
End SyncLock
‘触发另一线程取得机器信息
Dim HostInfo2 As New HostInfo(IMachineIP)
Dim HostThr2 As New Thread(New ThreadStart(AddressOf HostInfo2.sysInfo))
HostThr2.Start()
SyncLock GetType(HostInfoThreadCounter)
HostInfoThreadCounter.counter += 1 ‘启动线程数加1
End SyncLock
End If
SyncLock GetType(StoppCounter)
StopThr.AddStop()
End SyncLock
End Sub
End Class
‘该类只有一个方法,就是将停止的线程数减1
Class StoppCounter
Sub AddStop()
ThreadCounterStopped = ThreadCounterStopped + 1
End Sub
End Class
‘此类用于将已有的行插入DataSet
Class AddRow
‘第一个构造函数,以构造好的行为输入参数
Sub New(ByVal row As DataRow)
Try
ds.Tables(0).Rows.Add(row)
Catch ee As Exception
End Try
End Sub
‘第二个构造函数,以机器名用户名等字符串为参数,更新已有的行
Sub New(ByVal IP As String, ByVal user As String, ByVal hostname As String, ByVal Mac As String, ByVal domain As String, ByVal timeout As Char)
Dim RowTimeOut As DataRow
Try
For Each RowTimeOut In ds.Tables(0).Select("IP=''" & IP & "''")
RowTimeOut.Item("LastUID") = user
RowTimeOut.Item("Name") = hostname
RowTimeOut.Item("Mac") = Mac
RowTimeOut.Item("Domain") = domain
RowTimeOut.Item("Timeout") = timeout ''Set timeout flag to this item
Exit For ''just run once
Next
Catch er As Exception
End Try
End Sub
End Class
‘由于篇幅限制,这里省略了根据IP取得机器信息的类的代码。
Imports System.Threading ‘用于支持多线程
Imports System.Xml ‘用于分析XML格式的参数文件
Imports System.Data ‘用于保存结果到数据库
Module Module1
Public ds As New DataSet()
Public conn1 As SqlClient.SqlConnection ‘数据库连接
Public ipf As String ‘IP列表文件名
Public dbf As String ‘数据库信息文件
Public ThreadCounterStopped As Integer
Public StopThr As New StoppCounter()
Sub Main() ‘程序主
程序Dim machineIP As String
Dim iplistF As New Xml.XmlDocument()
Dim