2
\$\begingroup\$

I have an application coded in VB.NET that has a bunch of servers in a DataGridView and does a continuous asynchronous ping. If all the servers are up it refreshes great, but if one goes down and starts to time out it takes about 5-10 seconds before the program starts responding again. This program needs to ping all the servers at the same time. I need help with improving the performance of this code.

Private Sub cmdStartPing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartPing.Click

    Dim rc, myic As Integer
    Dim sname As String
    'Dim preply As PingReply

    pinging = 1
    cmdStopPing.Select()
    rc = DGV1.RowCount - 1

    If rc > -1 Then

        bCancel = False

        Do Until bCancel = True

            For myic = 0 To rc

                Application.DoEvents()

                If bCancel Then
                    bCancel = True
                    DGV1.BackgroundColor = Color.White
                    MsgBox("Pinging Stopped")
                    Exit For

                End If




                Try
                    sname = DGV1.Item(0, myic).Value

                    PingHost(sname)

                    If pingresults = "Success" Then
                        DGV1.Rows(myic).Cells(1).Value = "Success"
                        DGV1.Rows(myic).Cells(2).Value = roundtriptime
                        DGV1.Rows(myic).DefaultCellStyle.BackColor = Color.YellowGreen
                        DGV1.Refresh()
                    Else
                        DGV1.Rows(myic).Cells(1).Value = "No Reply"
                        DGV1.Rows(myic).Cells(2).Value = "Timed Out"
                        DGV1.Rows(myic).Cells(3).Value = currentdt
                        DGV1.Rows(myic).DefaultCellStyle.BackColor = Color.Red
                        DGV1.Refresh()
                    End If

                Catch ex As Exception
                    DGV1.Rows(myic).Cells(1).Value = "No Reply"
                    DGV1.Rows(myic).Cells(2).Value = "Timed Out"
                    DGV1.Rows(myic).Cells(3).Value = currentdt
                    DGV1.Rows(myic).DefaultCellStyle.BackColor = Color.Red
                    DGV1.Refresh()


                End Try


            Next

        Loop

    Else
        MsgBox("Please add at least one host to the datagrid to ping.")
    End If

End Sub

Stop ping:

Private Sub cmdStopPing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStopPing.Click
    bCancel = True
    pinging = 0
End Sub

Uses this function to ping:

Imports System.Net.NetworkInformation
Module AsyncPingHost
'Function PingIPAdress(ByVal IPAdress As String)
Function PingHost(ByVal host As String)
    Dim ping As Ping
    Dim preply As PingReply
    ping = New Ping

    Try
        preply = ping.Send(host)
        roundtriptime = preply.RoundtripTime
        If preply.Status = IPStatus.Success Then
            pingresults = "Success"
        Else
            pingresults = "Failed"

        End If
    Catch ex As Exception
        pingresults = ex.Message
    End Try


End Function
\$\endgroup\$
3
\$\begingroup\$

I have a similar problem (but I have to use PSLoggedOn.Exe which times out in 10-15 seconds).

To mitigate the problem I use WMI to Ping.

I will then do PSLoggedOn only if the result was a valid IPnumber.

WmiPingStatus is perhaps a little less reliable because it is faster. But if You don't get an answer you will get an answer the next loop.

    Friend Function WmiPingStatus(computer As String) As String
    Dim searcher As New ManagementObjectSearcher("SELECT ProtocolAddress FROM Win32_Pingstatus WHERE Address = '" & computer & "'")
    For Each wmiObj As ManagementObject In searcher.Get()
        For Each prop As PropertyData In wmiObj.Properties
            If prop.Name.Equals("ProtocolAddress", StringComparison.InvariantCultureIgnoreCase) Then Return wmiObj(prop.Name.ToString).ToString
        Next
    Next
    Return ""
End Function

Your function PingHost is not async but You can do it async if You use :

Await preply = SendAsync(host, Object)

But that is only part of the problem becasue You still have to wait for the answer. Async is allways good because it will use less resources when Waiting. And while You wait you could perhaps do for example 5 Task in "Parallell". But then You still have to wait for the longest query to end. So perhaps WmiPingStatus could be useful also for You

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.