Collections that are thread safe with vb.net

Good Morning all! Some of you know I have started writing a fictional book. Last night I finished up completing the cast of characters the book will use (though I am sure there will be other changes) and thought how I will begin the book. Now if you know any literary agents please send them my way please! If you have enjoyed my writing up to this point. 🙂

Anyway, back to the real world…..We needed to prevent changes being made by other threads when enumerating through our collection. Basically this prevented the threads from making changes while enumerations were occurring. It uses the concept of SyncLock to prevent these changes from being made.

Make it a great day!

Private Sub TimerFired() – happens every 1 second
    Dim x As Integer = 0
    SyncLock MySync.SyncObject
        Do While x <> MyList.Count – 1
            ODC = MyList(x)
            … condition 1: AddToMyList(ODC)
            … condition 2: RemoveFromMyList(x)
            x = x + 1
        Loop
    End SyncLock
End Sub
Private Sub AddToMyList(var)
    SyncLock MySync.SyncObject
        MyList.Add(var)
    End SyncLock
End Sub
Private Sub RemoveFromMyList(index)
    SyncLock MySync.SyncObject
        MyList.RemoveAt(index)
    End SyncLock
End Sub

Technorati Tags: ,,,,,,,,,,,,

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: