Results 1 to 2 of 2

Thread: DataGridView not updating correctly with Asynchrouns Continuous Ping

  1. #1
    cschneider is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    May 2012
    Posts
    1
    Reputation
    0

    DataGridView not updating correctly with Asynchrouns Continuous Ping

    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..

    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 Buttong:

    Code:
    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:

    Code:
    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

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,177
    Reputation
    2368
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking