DataGridView not updating correctly with Asynchrouns Continuous Ping

cschneider

New member
Joined
May 3, 2012
Messages
1
Programming Experience
5-10
I have an application I have coded in vb .net that has a bunch of servers in a DataGridView and does a continues 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 teh same time. the code is below. what am I doing wrong. Any help would be greatly appreciated..
I also have a context menu that when I rightclick on an item in the datagridview i get a menu that is where i see it's freezign the app on the time out. The program also does a continues loop until I click a "Stop Ping" button. All code is below..

VB.NET:
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 Buttong:

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

Asynchrounds Pings Function:

VB.NET:
Imports System.Net.NetworkInformation 
Module AsyncPingHost 
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
 
Ping Class (System.Net.NetworkInformation)
SendAsync method is the async (non-blocking) version of the Send method. The response will then be from PingCompleted event.
Alternative if you expect immediate response is to set a shorter than default timeout for Send method.
 

Latest posts

Back
Top