Click here to Skip to main content
14,449,906 members
Rate this:
Please Sign up or sign in to vote.
See more:
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?
Posted
Updated 7-Feb-20 6:04am
v2
Comments
Richard MacCutchan 7-Feb-20 11:04am
   
You need to synchronise your threads so that only one thread can reorder/resize the array at any time.
Member 12561559 7-Feb-20 12:08pm
   
Cheers Richard, I thought it would be that, though Phil seems to have a threadsafe method below, Ive googled it, it doesnt seem to get used a lot, perhaps most people dont use threads and lists but I'll take a look at his solution and if I cant get that method around my head, I'll go for a wait (though I'd prefer not to as timings are important on these threads) - but at least there are a couple of things I can look at. Cheers and have a good weekend :)

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Maybe you would be interested to use a thread-safe list instead, to avoid running into this random race condition?
Thread-Safe Collections | Microsoft Docs[^]
   
Comments
Member 12561559 7-Feb-20 12:10pm
   
Thanks Phil - I'll take a look at that, never heard of these collections before, hopefully I can get them to work with what I have - will be better than a wait state on the threads due to timings being important in what I am trying to achieve - thanks and have a good weekend !
phil.o 7-Feb-20 13:14pm
   
You too! You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100