Results 1 to 25 of 25

Thread: ping using vb-

Threaded View

  1. #19

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb-

    okay thanks will try that ! what about adding a progress bar ? could this be easily done ?

    im guessing its something like this but not sure what to set value to, also not sure where to put code because everywhere i try i get crossthread errors ?

    Code:
    ProgressBar1.Minimum = 0     'sets minimum value of progress bar
            ProgressBar1.Maximum = Counter 'maximum value of progres
    
            ProgressBar1.Value = ??   ' Set up the progress bar's properties
    also tried changing from richtextbox to datagridview but im having trouble with the loop.
    heres code:

    Code:
    Imports System.Data.OleDb
    
    Public Class Form1
    
        ''' <summary>
        ''' Represents a network switch (or any device with an IP) that is in the database
        ''' </summary>
        Private Class NetworkSwitch
            Private _IPAddress As String
            Private _ID As Integer
            Private _Result As Boolean
    
            Public Property IPAddress() As String
                Get
                    Return _IPAddress
                End Get
                Set(ByVal value As String)
                    _IPAddress = value
                End Set
            End Property
    
            Public Property ID() As Integer
                Get
                    Return _ID
                End Get
                Set(ByVal value As Integer)
                    _ID = value
                End Set
            End Property
    
            Public Property Result() As Boolean
                Get
                    Return _Result
                End Get
                Set(ByVal value As Boolean)
                    _Result = value
                End Set
            End Property
    
        End Class
    
        'Set up some variables that we will use from various functions/subs
        Dim i As Integer
    
        Private PingList As List(Of NetworkSwitch)
        Private Counter As Integer = 0
        Private ConnString As New OleDbConnectionStringBuilder("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test\pingDB.mdb;User Id=admin;Password=;")
        'This is the Delegate that will be used to pass data between the threads safely
        Private Delegate Sub PingComplete(ByVal sw As NetworkSwitch)
    
    
        Private Sub Pingbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pingbtn.Click
            Counter = 0 'reset the counter in case this is not the first time the ping button has been clicked
            Pingbtn.Enabled = False  'Disable the ping button so it cannot be pressed again yet
            btnExit.Enabled = False
            Dim bgthread As New Threading.Thread(AddressOf BG_CollectData)  'Create a new background thread
            bgthread.IsBackground = True  'Make the thread end if the program is closed
            bgthread.Start()  'Start the background thread
        End Sub
    
        ''' <summary>
        ''' This is the routine that is executed when bgthread.Start() is called in the event handler above
        ''' It collects information from the Access database and stores it in a list, then kicks off another
        ''' background thread (from the ThreadPool) that sends the ping
        ''' </summary>
        Private Sub BG_CollectData()
            'Create our connection that will be used to connect to the Access database
            Dim AccessConnection As New OleDbConnection(ConnString.ConnectionString)
    
            'Create the command that will be used to select all of the data in the IP and ID columns
            Dim SelectCommand As New OleDbCommand("SELECT * FROM PingTable", AccessConnection)
    
            'Create a new instance of a list of our NetworkSwitch class
            PingList = New List(Of NetworkSwitch)
    
            'Open the connection to the database
            AccessConnection.Open()
    
            'Use a DataReader to access the data that the SELECT command returns
            Dim Reader As OleDbDataReader = SelectCommand.ExecuteReader
            'Start to loop through the results
            'This loop will be restarted for each row in the database
            Do While Reader.Read
                'Create a new instance of our NetworkSwitch class and populate the ID and IP properties
                'with data from the current row in the database that we are looping through
                Dim CurrentSwitch As New NetworkSwitch
                With CurrentSwitch
                    .ID = Reader("ID")
                    .IPAddress = Reader("IPAddress")
                End With
                'Add the new instance of the NetworkSwitch to our list
                PingList.Add(CurrentSwitch)
            Loop 'Start the loop again for the next record in the database
    
            'Close the connection because we have now processed all rows in the database
            AccessConnection.Close()
    
            'Give the threadpool a limit so that no more than 30 threads are running at the same time
            Threading.ThreadPool.SetMaxThreads(30, 400)
    
            'Loop through our list of NetworkSwitch instances and start (well, queue up) a new background
            'thread for each item in the list and passes it the current item in the loop (which is a NetworkSwitch object)
            For Each switchobject As NetworkSwitch In PingList
                Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(AddressOf DoPing), switchobject)
            Next
        End Sub
    
        ''' <summary>
        ''' Performs the ping and updates the Result property of the NetworkSwitch object that was passed to this 
        ''' </summary>
        Private Sub DoPing(ByVal Switch As NetworkSwitch)
            Dim Result As Net.NetworkInformation.PingReply
            Dim SendPing As New Net.NetworkInformation.Ping
            Result = SendPing.Send(Switch.IPAddress)
            If Result.Status = Net.NetworkInformation.IPStatus.Success Then
                Switch.Result = True
    
                ' <<<<<<----change Font Color to green/red ??
            Else
                Switch.Result = False
            End If
    
            UpdateDatabase(Switch)
            Counter += 1
            Me.Invoke(New PingComplete(AddressOf UI_PingComplete), Switch)
        End Sub
    
        ''' <summary>
        ''' This is executed each time a ping completes and the database has been updated.
        ''' It is executed on the UI thread because we used Me.Invoke to call it so it can access
        ''' controls on the UI thread without a problem
        ''' </summary>
        Private Sub UI_PingComplete(ByVal sw As NetworkSwitch)
    
            Dim dgvr As DataGridViewRow
            Dim i As Integer
            For i = 0 To PingList.Count
    
                Application.DoEvents()
    
                dgvr = New DataGridViewRow      'Creates a new row            
                DataGridView1.Rows.Add(dgvr)    'Add the row             
    
    
    
    
    
                DataGridView1.Rows.Item(i).Cells(0).Value = sw.IPAddress
    
                DataGridView1.Rows.Item(i).Cells(1).Value = sw.ID
    
                DataGridView1.Rows.Item(i).Cells(2).Value = sw.Result
    
                DataGridView1.Rows.Item(i).Cells(3).Value = Now.ToString() 'show the last time checked with Now.ToString() method
    
    
    
                '   logbox.AppendText(sw.IPAddress & " ping complete - " & sw.Result & vbNewLine) 'update our log textbox
                If Counter = PingList.Count Then
                    ' logbox.AppendText("Finished all tests" & vbNewLine)
                    Pingbtn.Enabled = True 're-enable the ping button
                    btnExit.Enabled = True
                    PingList = Nothing 'clean up
                End If
            Next
        End Sub
    
        ''' <summary>
        ''' Updates the relevant row in the database for the switch that is passed to this method
        ''' </summary>
        Private Sub UpdateDatabase(ByVal sw As NetworkSwitch)
    
            Dim AccessConnection As New OleDbConnection(ConnString.ConnectionString)
            Dim UpdateCommand As New OleDbCommand("UPDATE PingTable SET Result=@Result, LastTested=@CurrentTime WHERE ID=@ID", AccessConnection)
            With UpdateCommand.Parameters
                .Add("@Result", OleDbType.Boolean).Value = sw.Result
                .Add("@CurrentTime", OleDbType.Char).Value = Now.ToString
                .Add("@ID", OleDbType.Integer).Value = sw.ID
            End With
            AccessConnection.Open()
    
            UpdateCommand.ExecuteNonQuery()  'Execute the command that updates the database
    
            AccessConnection.Close()
    
    
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            End
        End Sub
    End Class
    Last edited by markhorgan1; Aug 11th, 2009 at 05:53 AM.

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