CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2015
    Posts
    1

    Using Multithread Ping Requests to Ping Hosts from an Array

    I need to use Multithread Ping Requests to Ping Hosts from an Array

    I have tried doing it but i get this error after i click on my ping button "Index was out of range. Must be non-negative and less than the size of the collection."

    My code is below, Please can someone help

    Code:
    Imports System.Threading
    Imports System.Threading.Tasks
    Imports System.Net.NetworkInformation
    
    Partial Public Class Form2
        Private fThread As Thread
        Private fThread2 As Thread
    
        Public Delegate Sub AddRowDelegate(ByVal column As Integer)
        Delegate Sub CheckOnlineDelegate(ByVal rowindex As Integer)
        Delegate Sub SetOnlineDelegate(ByVal rowindex As Integer)
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            fGrid.Columns.Add("Ip", "Ip")
            fGrid.Columns.Add("Ping", "Ping")
            fGrid.Columns(0).Width = 200
            fGrid.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    
            Dim IpArray() As String = {"rockstardevelopment.co.za", "gospelofdanceacademy.co.za", "rkaconsulting.co.za"}
    
            For i As Integer = 0 To IpArray.Length - 1
                Dim myRowIndex = fGrid.Rows.Add()
                fGrid.Rows(myRowIndex).Cells(0).Value = IpArray(myRowIndex)
            Next
        End Sub
    
        Private Sub ThreadProc()
            Parallel.For(0, 5, Sub(b)
                                   Do While Not done.Contains(b)
                                       fGrid.Invoke(New AddRowDelegate(AddressOf AddRow), New Object() {b})
                                       Thread.Sleep(300)
                                   Loop
                                   fGrid.Invoke(New SetOnlineDelegate(AddressOf SetOnline), New Object() {b})
                               End Sub)
        End Sub
    
        Private Sub ThreadProc2()
            Parallel.For(0, 5, Sub(b)
                                   CheckOnline(b)
                               End Sub)
        End Sub
    
    
        Private Sub AddRow(ByVal rowindex As Integer)
            If fGrid.Rows(rowindex).Cells(1).Value Is Nothing OrElse fGrid.Rows(rowindex).Cells(1).Value.ToString.Contains(".....") OrElse Not fGrid.Rows(rowindex).Cells(1).Value.ToString.Contains("Pinging") Then
                fGrid.Rows(rowindex).Cells(1).Value = "Pinging..."
            Else
                fGrid.Rows(rowindex).Cells(1).Value = fGrid.Rows(rowindex).Cells(1).Value.ToString & "."
            End If
            fGrid.Rows(rowindex).Cells(1).Style.BackColor = Drawing.Color.White
        End Sub
    
        Private success As New List(Of Integer)
        Private done As New List(Of Integer)
    
        Private Sub CheckOnline(ByVal rowindex As Integer)
            Dim _ping As New Ping
            Try
                Dim _pingreply = _ping.Send(fGrid.Rows(rowindex).Cells(0).ToString, 2000)
                If _pingreply.Status = IPStatus.Success Then
                    SyncLock success
                        success.Add(rowindex)
                    End SyncLock
                End If
            Catch ex As Exception
            End Try
            SyncLock done
                done.Add(rowindex)
            End SyncLock
        End Sub
    
        Private Sub SetOnline(ByVal rowindex As Integer)
            If Not success.Contains(rowindex) Then
                fGrid.Rows(rowindex).Cells(1).Value = "Offline"
                fGrid.Rows(rowindex).Cells(1).Style.BackColor = Drawing.Color.Red
            Else
                fGrid.Rows(rowindex).Cells(1).Value = "Online"
                fGrid.Rows(rowindex).Cells(1).Style.BackColor = Drawing.Color.Green
            End If
        End Sub
    
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            done = New List(Of Integer)
            success = New List(Of Integer)
            fThread = New Thread(New ThreadStart(AddressOf ThreadProc))
            fThread.IsBackground = True
            fThread.Start()
            fThread2 = New Thread(New ThreadStart(AddressOf ThreadProc2))
            fThread2.IsBackground = True
            fThread2.Start()
        End Sub
    End Class

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Using Multithread Ping Requests to Ping Hosts from an Array

    Place a break point in the top of the function called by the button. Then step through. Look for a tutorial on debugging.
    That will give you a hint at what part is broken.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Using Multithread Ping Requests to Ping Hosts from an Array

    It means that you are trying to store or access an array item which is not in the list, or the list is too small to host another entry into the array

  4. #4
    Join Date
    Sep 2015
    Posts
    1

    Re: Using Multithread Ping Requests to Ping Hosts from an Array

    Hello,
    Would you rather send me the array and I will ping for you everyday (rain style...)

    Why are you attempting to utilize this forein style of coding instead of MS DOS?

    Message me for details thanks...

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Using Multithread Ping Requests to Ping Hosts from an Array

    MS DOS? Really? MS-DOS can't even be installed on most computers now, no floppy drives, large HDD to much ram, other hardware not supported by dos.

    Perhaps you meant running from the command line under Windows?
    Always use [code][/code] tags when posting code.

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

Featured