Results 1 to 6 of 6

Thread: Ping using VB (performance issues)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Ping using VB (performance issues)

    i have this code to ping a list of over 400 switches , the program works fine but for some reason the first time i ping all the switches alot of the attempts time-out(even tho they shouldnt) which makes the program alot slower , if i click ping again less of the attempts time-out(and as a result the program runs faster) and if i click it a third time i usually get a completely accurate result with all the switches pinging back "success" (except for a few which i know for a fact are down anyway) any ideas why this is ??

    Code:
    Public Class SwitchMonitor
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
    
            Label1.Visible = True
            Label2.Visible = True
            Label3.Visible = True
            Label4.Visible = True
            Label5.Visible = True
    
    
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\Network Map.mdb"
            con.Open()
    
            sql = "SELECT * FROM tblSwitch"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "monitor")
    
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Ping = New Net.NetworkInformation.Ping
            Dim dgvr As DataGridViewRow
    
            ProgressBar1.Minimum = 0                                      'sets minimum value of progress bar
            ProgressBar1.Maximum = ds.Tables("monitor").Rows.Count - 1    'sets maximum value of progress bar
    
            For i = 0 To ds.Tables("monitor").Rows.Count - 1
    
                Application.DoEvents()
    
                ' Set up the progress bar's properties
                ProgressBar1.Value = i
    
    
                dgvr = New DataGridViewRow 'Creates a new row            
                DataGridView1.Rows.Add(dgvr) 'Add the row             
    
                Try
    
                    pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(5)) 'Sends the ping to the current item                
    
    
    
                    DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(1) 'read in switch name from column 2
    
                    DataGridView1.Rows.Item(i).Cells(1).Value = ds.Tables("monitor").Rows(i).Item(5) 'Updates the value of the new Item                
    
                    DataGridView1.Rows.Item(i).Cells(2).Value = pReply.Status  'The value of the status returned                 
    
                    DataGridView1.Rows.Item(i).Cells(3).Value = Now.ToString() 'show the last time checked with Now.ToString() method
    
    
    
    
                    If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell         
    
                        DataGridView1.Rows.Item(i).Cells(2).Style.BackColor = Color.Red
                    Else
                        DataGridView1.Rows.Item(i).Cells(2).Style.BackColor = Color.Green
                    End If
    
                    ds.Tables("monitor").Rows(i).Item(6) = pReply.Status 'Write in the original datatable
                    ds.Tables("monitor").Rows(i).Item(7) = Now.ToString() 'added code for date/time
                    da.Update(ds, "monitor") 'Updates the database
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
            Next
        End Sub
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\Network Map.mdb"
            con.Close()
            End
        End Sub
    
        Private Sub SwitchMonitor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class

  2. #2
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: Ping using VB (performance issues)

    I would suggest you to use separate threads for pinging each switch. In case, there is no response from any of the switch, ur app would continue pinging other switches in separate thread.
    Microsoft Techie

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Ping using VB (performance issues)

    As well as spawning a few extra threads, you might also want to consider having a slight time offset between each ping so that it is not trying to send a lot of pings all at the same time.
    You need to do a bit of reading up on multi-threading because it is not just a simple change in your code, you will have to change how you are updating the user interface (because the UI runs on a separate thread that only it can access) amongst other things. jmcilhinney wrote a good in depth tutorial on background threads, you can find it in the codebank on these forums. There is one here about using background workers (which are a component that wrap up the functionality of a background thread and add some handy methods and event handlers): http://www.vbforums.com/showthread.php?t=471889
    Having said that, a background worker is probably not appropriate in your situation because you need to be launching multiple threads to handle all these pings. Using the System.Threading.Thread and ThreadPool classes are probably your best bet.

    EDIT: here is the one about accessing controls from background threads: http://www.vbforums.com/showthread.php?t=498387
    Last edited by chris128; Jul 20th, 2009 at 05:24 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: Ping using VB (performance issues)

    thanks will start lookin at multi-threading so ! any ideas why it takes around 3 pings to get an accurate result (i.e why alot of switches time out on the first ping but if i run program again they succed) is it just because im only using the one thread and its so long ??

    also would it be difficult to change the code to just have the loop running as one background thread as you mentioned earlier to make it a little more responsive (just to allow it to be minimized while it runs basically) if so would having it run as a background thread slow it down in anyway ?

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Ping using VB (performance issues)

    Well you could have the loop running as one background thread but the idea that pvbangera and myself were suggesting was to have more than one background thread so that you can do more than one ping at a time. Doing that will make your UI more responsive because the ping is no longer being done in your UI thread so you will be able to minimise it or whatever at any time.
    Oh and using multiple threads will not slow it down, it will in your case make the whole process complete much faster because you are not having to wait for each ping before another one can be sent

    I've created an example for you that uses one background thread to gather the data from the Access database and then once it has collected all of the necessary info from the database it loops through the in-memory copy of the data and sends a ping to all of the IPs in the collection using the ThreadPool. Each time a ping completes (either successfully or not) it adds 1 to a counter and checks to see if the counter now equals the same amount that was in the list of IPs to ping, if so then we know it is the last ping to respond so we can output a message saying "all tests finished" and do whatever you like. Will upload it in a minute after I have added comments to the code.

    EDIT: See file attached to this post
    Attached Files Attached Files
    Last edited by chris128; Jul 20th, 2009 at 10:34 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Ping using VB (performance issues)

    Attached example project file to previous post, let me know if you have any questions (obviously you need to change a few column names and the path to the database etc etc but its more for you to learn from rather than just start using instead of what you have got already. Also there is no error handling at all in there so if you are going to try and use my version then you need to at least add a few Try, Catch, Finally blocks
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width