0

下午所有,

我正在用 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

可能有更好的方法来完成/编写此任务,但以上只是一个开始。任何帮助深表感谢。

问候贝蒂。

4

2 回答 2

0

您正在为每个 IP 地址调用 Ping.SendAsync(),这是一种异步方法,因此响应不一定会按照它们发送的顺序返回。

您可以改用 Ping.Send() 方法吗?

于 2013-06-11T14:22:31.143 回答
0

据我所知,您有两种选择:

  1. 缓冲结果。收集所有结果,全部完成后才显示。这可能是这两个选项中最快的执行方式,但可能会延迟显示结果。
  2. 排队 ping 并等待一个完成,然后再开始下一个。

您正在使用SendAsync这意味着,它在设计上是异步的。这是您在代码中做出的选择。如果您希望保持它们有序(即) ,您可以使用同步命令。Send或者,如果出于某种原因您仍然希望它们是异步的,那么您还可以使用信号量和互斥锁之类的东西来停止下一次迭代的执行……这里有很多选项,您只需要选择最适合您的选项。

于 2013-06-11T14:24:31.027 回答