I seem to get this error "occasionally", more often than not when I stop some threads and restart them or others, on top threads that are already running..
i.e.
I have 15 threads running, i stop thread 12 and 14 for a few seconds, then restart 12 and 14 and then the error "may" happen.
The details....
I have a List Of T, a Class with properties that I add onto, from a number of running threads.
As it builds build up ThreadResults with data, I have a main form ticker that does some work on the ThreadResults on row(0), with a Do Until ThreadResults.Count=0
at the end of the first DO Loop, before the loop command, I have a
ThreadResults.RemoveAt(0)
which removes the item from the bottom of the list, .NET is then supposed to shuffle the list around, bringing items 1 to 0, 2 to 1, 3 to 2 etc.. and ThreadResults.Count goes down reduces by one item and the loop keeps chipping at RemoveAt(0) until the count is zero, then it exits the loop altogether, performs a thread.slep of a few seconds and begins the loop again (all the while threads are adding one item to the list) when it needs to.
Sometimes I get no errors, for hours, other times - more commonly it happens when I stop some threads and then restart them while other threads continue in the background, as per my statement above.
I have a feeling the loop is quicker than the list reshuffle - even in debug mode where I can trap the error , I go and look at ThreadResults(0).FrequencyID and I will get a value, but the error persists, under debug mode , a removeat(0) causes the error to reappear.
Is there a method of managing this? Should I even be using a list of properties and maybe switch to a different type of list because of the threading. Should I perhaps put some kind of wait in my sub-threads when the main thread hits the remove at and for it to only remove, when I know all my threads are paused, before continuing.
I hope that made sense ! :) and Thanks for any suggestions.
I have included the list of with some of the properties (there are about 10 altogether) - should I perhave use an in-memory dataset to manage it and query that in a loop, rather than a list of, that doesnt seem to be thread-safe?
<pre>Friend ThreadResults As New List(Of Thread_History_Class)()
Public Class Thread_History_Class
Private _FrequencyID As Int64 = 0
Private _Port As Int32 = 0
Private _ResultTxt As String = ""
Private _Success As Boolean = False
Friend Sub New()
' do nothing
End Sub
Friend Sub New(ByVal FrequencyID As Int64,
ByVal Port As Int32,
ByVal ResultTxt As String,
ByVal Success As Boolean)
Friend Property Success() As Boolean
Get
Return _Success
End Get
Set(value As Boolean)
_Success = value
End Set
End Property
Friend Property ResultTxt() As String
Get
Return _ResultTxt
End Get
Set(value As String)
_ResultTxt = value
End Set
End Property
Friend Property Port() As Int32
Get
Return _Port
End Get
Set(value As Int32)
_Port = value
End Set
End Property
Friend Property FrequencyID() As Int64
Get
Return _FrequencyID
End Get
Set(value As Int64)
_FrequencyID = value
End Set
End Property
End Class
What I have tried:
Should I use an in-memory dataset perhaps, rather than a list or make my threads wait until RemoveAt(0) has been compleed - I was just going to use a thread.sleep but perhaps there is a better way of managing a list under thread circumstances?