下午所有,
我正在用 VB 编写我的第一个控制台应用程序。我正在开发的应用程序本质上是 ping 一个网站列表,然后写出状态。
我的问题是以下...
有没有办法让我订购我希望在我的 VB 中 ping 的网站。目前我只是列出这个,我在 ping 命令中写这些站点的顺序并不重要,似乎可以撤回写出最快响应的状态。问题是我有几个站点位于同一个网络框上,并且在写出状态时状态消息似乎搞砸了。我认为这是由于它们具有相同的响应时间。
请找到我一直在使用的代码...
'Imports ping class
Imports System.Net.NetworkInformation
Module Module1
Sub Main()
Console.Title = ("Daily Checks PING to Check Current Status")
Dim myPingList As New List(Of String) 'A LIST TO HOLD SOME STRINGS
Dim myPing As Ping = Nothing 'PING CLASS
Dim timeout As Integer = 100000 'TIMEOUT FOR PING REQUESTS
'ADD A BUNCH OF THINGS TO PING
myPingList.Add("website1")
myPingList.Add("website3")
myPingList.Add("website2")
'LOOP THE STRING LIST OF PLACES WE WANT TO PING
For Each ItemToPing As String In myPingList
'CREATE A NEW PING OBJECT
myPing = New Ping
'ADD EVENT HANDLER FOR THE PING COMPLETED EVENT
'PingResult IS THE SUB ROUTINE BELOW
AddHandler myPing.PingCompleted, _
AddressOf PingResult
'TELL USER WHAT IS BEING PINGED
'Console.WriteLine("Sending PING Request To: " & ItemToPing)
'SEND ASYNC PING REQUEST
'myPing.SendAsync(ItemToPing, timeout, Buffer, ItemToPing)
myPing.SendAsync(ItemToPing, timeout, ItemToPing)
Next
'WAIT FOR A KEYPRESS TO CLOSE CONSOLE WINDOW
Console.WriteLine("")
Console.WriteLine("Critical Systems Ping Results:")
Console.ReadKey(True)
End Sub
'THE PING RESULT THAT WILL FIRE WHEN A PING COMPLETES
Private Sub PingResult(ByVal sender As Object, ByVal e As System.Net.NetworkInformation.PingCompletedEventArgs)
'OUTPUT TO USER, e.UserState IS WHAT WE PASSED IN AS THE UserToken (ItemToPing) IN THE ABOVE CODE
Console.WriteLine("")
Console.WriteLine("***********************************")
Console.WriteLine("Application: " & e.UserState.ToString)
''IF THERE WAS AN ERROR THEN OUTPUT INFORMATION ABOUT THE ERROR
''OTHERWISE OUTPUT INFORMATION ABOUT THE RESULT OF THE PING REQUEST
''THE EVENTARGS e HAVE A LOT OF INFORMATION IN THE e.Reply VARIABLE
If e.Error IsNot Nothing Then
Console.WriteLine("Error: " & e.Error.Message)
If e.Error.InnerException IsNot Nothing Then
Console.WriteLine("More Info: " & e.Error.InnerException.Message)
End If
Else
Console.WriteLine("Status: " & e.Reply.Status.ToString)
Console.WriteLine("Round Trip Time: " & e.Reply.RoundtripTime.ToString)
Console.WriteLine("Response Buffer Length:" & e.Reply.Buffer.Length.ToString)
End If
Console.WriteLine("***********************************")
'THE SENDER IN THE ARGUMENTS IS THE PING OBJECT THAT WE CREATED
'SO NOW REMOVE THE EVENT HANDLER LISTENER AS IT IS NO LONGER NEEDED
'AND CALL Dispose() ON THE PING CLASS TO CLEAN UP UNMANAGED RESOURCES
With DirectCast(sender, Ping)
RemoveHandler .PingCompleted, AddressOf PingResult
.Dispose()
End With
End Sub
End Module
可能有更好的方法来完成/编写此任务,但以上只是一个开始。任何帮助深表感谢。
问候贝蒂。