Results 1 to 3 of 3

Thread: [RESOLVED] Threaded Ping

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    137

    Resolved [RESOLVED] Threaded Ping

    Hi

    Ive got an app that holds a list of site we have, At each site there are a few devices.

    The idea is when they click on a site all the details for the site are loaded and then a second thread sits and pings each device to see if its live. Im trying to do this in a seperate thread so if they click another site while its still pinging them it doesn't wait before it switches site.

    This all seems to be workling fine except if you click through the sites quickly, when you do that it will eventually come up with
    {"Collection was modified; enumeration operation might not execute."}
    Ive been struggling to work out how to fix this so any help would be great

    the error flags up on the "Next" line in "ThreadedPing"

    Thread stuff
    Code:
        Public Sub ThreadedPing(ByVal dt As DataTable)
            Try
    
                Dim dr As DataRow
    
                For Each dr In dt.Rows
                    dr("Ping") = PingaLing(dr("DeviceIP"))
                Next
                frmMain.lstS1AttDev.DataSource = dt
                frmMain.lstS1AttDev.Splits(0).DisplayColumns(0).Width = frmMain.lstS1AttDev.Width / 4
                frmMain.lstS1AttDev.Splits(0).DisplayColumns(1).Width = frmMain.lstS1AttDev.Width / 4
                frmMain.lstS1AttDev.Splits(0).DisplayColumns(2).Width = frmMain.lstS1AttDev.Width / 5
                frmMain.lstS1AttDev.Splits(0).DisplayColumns(3).Width = 0
                frmMain.lstS1AttDev.Splits(0).DisplayColumns(4).Width = frmMain.lstS1AttDev.Width / 5
    
            Catch abortException As ThreadAbortException
                Console.WriteLine(CType(abortException.ExceptionState, String))
            End Try
    
        End Sub
    
        Public Function PingaLing(ByVal strIPAdd As String) As String
            Dim dtstart As Date
    
            dtstart = Now
    
            Try
                My.Computer.Network.Ping(strIPAdd)
    
                Dim runLength As Global.System.TimeSpan = Now.Subtract(dtstart)
    
                PingaLing = runLength.Milliseconds & " ms"
            Catch ex As Exception
                PingaLing = "Cannot Find Host!"
            End Try
    
        End Function

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Threaded Ping

    Welcome to the world of multithreading.
    You try to enumerate a collection and at the same time another thread changes it in some way.

    Before making some changes be sure to sync lock your thread critical object like this:

    vb.net Code:
    1. SyncLock  dt
    2.     For Each dr In dt.Rows
    3.         dr("Ping") = PingaLing(dr("DeviceIP"))
    4.     Next
    5. End SyncLock

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    137

    Re: Threaded Ping

    cheers.

    seems to stop the problem

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