Results 1 to 7 of 7

Thread: How to make LINQ statement ThreadSafe?

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    268

    How to make LINQ statement ThreadSafe?

    I have a LINQ statement that returns only certain objects from a collection. I keep getting "collection has been modified" errors, because other threads are modifying the "Checks" collection at the same time that this LINQ statement is trying to pull some numbers.

    Here's the procedure where I keep getting the "collection was modified" errors:

    vb.net Code:
    1. Public Function GetActualCheckCount() As Int32
    2.        
    3.         Dim RetryCount As Int32 = 0
    4. Retry:  Try
    5.  
    6.              Return (From x In Checks Where Not x.Status = 3).Count
    7.  
    8.         Catch ex0708 As Exception
    9.             If ex0708.Message.ToLower.Contains("collection was modified") AndAlso RetryCount < 8 Then
    10.                 RetryCount += 1
    11.                 GoTo Retry
    12.             End If
    13.         End Try
    14.     End Function
    Last edited by GregSenne; Aug 20th, 2012 at 01:51 PM.

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