|
-
Jul 20th, 2009, 03:38 AM
#1
Thread Starter
Lively Member
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
-
Jul 20th, 2009, 03:44 AM
#2
Fanatic Member
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.
-
Jul 20th, 2009, 05:18 AM
#3
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.
-
Jul 20th, 2009, 06:40 AM
#4
Thread Starter
Lively Member
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 ?
-
Jul 20th, 2009, 09:29 AM
#5
-
Jul 20th, 2009, 10:36 AM
#6
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|