|
-
Aug 6th, 2009, 09:36 AM
#1
Thread Starter
Lively Member
ping using vb-
Sorry for new thread--i have this code,basically a loop to ping a list of ip's return the results and save them to an access db:
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 'sets the visible properties to advice labels that display after clicking ping button
Label2.Visible = True
Label3.Visible = True
Label4.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 in network map db
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 based on ping result
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 'Error Handling
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
The problem is that for some reason when i first run the code in the morning all the switches time out and as there are over 400 switches this takes over 25mins during which the program remains unresponsive, but if i then run the code a second time it works perfectly and all the switches that are up ping back and it only takes around 4mins (the reason it takes 4mins is because there are a number of switches in the db which i know are down and each of these take 5secs to time out). Its as if the program needs a warm up run ? anyone able to offer an explanation ?? - i have been told to use multiplethreads and background threads but im new to coding and my skills are very limited so im hoping if im just able to identify the problem i might be able to find an easier way around it-besides from what i gather the two suggestions mentioned above will only speed up the program which is not what im looking for,im just wondering WHY the code behaves differently the first time i run it ??-cant figure it out would appreciate any ideas cheers
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
|