Missing index in arraylist

ramanaths

Member
Joined
May 31, 2005
Messages
19
Programming Experience
Beginner
Hi

My app has multiple threads reading data from pipe and storing it in an arraylist. When parsing the arraylist i got an error... seemed like a null value that's crept in. The thread processing continued and i could see data being stored in the arraylist.

Looking into it further threw up a weird problem. An index position in the arraylist was missing!! :eek: Well I thought maybe I could expect null value to be stored at some positions but a missing index position seems strange.:confused:

The arraylist is named DataHolder.
What I see is DataHolder[1281]='abc' and then DataHolder[1283]='xyz'. There is no DataHolder[1282].

Any ideas why this happens? Is this a common occurance?
 
So what happens if you execute this line:
VB.NET:
MessageBox.Show(DataHolder(1282).ToString())
Also, have you synchronised access to your ArrayList? The help documentation specifically says that instance members are NOT thread-safe. Firstly you should be getting a thread-safe version of your ArrayList via the Synchronized method, then you should be wrapping an multi-line operations you perform on it in SyncLock blocks. Multi-threading is error-prone if not done properly.
 
For example:
VB.NET:
        Dim list As New ArrayList
        Dim threadSafeList As ArrayList = ArrayList.Synchronized(list)

        SyncLock threadSafeList.SyncRoot
            For Each item As Object In threadSafeList
                MessageBox.Show(item.ToString())
            Next item
        End SyncLock
 
Back
Top