I have a bug in my code that throw an IndexOutRange exception while running:
string st = Playsets.Hashtableofvariables[1000].ToString();
As you can easily imagine Hashtableofvariables is an hashtable of variable ??? (sorry for my bad imagination in naming convention)
The value stored at key 1000 is modified by a separate thread like the following one:
public void Thread_Rx()
{
while (true)
{
HtV[1000]=new_value;
Thread.Sleep(10);
}
}
The exception occurs after 20 to 30 seconds. When VS stops, it always stops at the same line and when I list all the entry in the Hastable I see one with key 1000. So I do not know what is wrong and how to debug that. Any idea?
Exactly architect the hashtable seems valid, and key 1000 is stored at index 570 in the current case (I donāt know how CLR compute the hash code but it seems to be always the same index for this <key,value> pair. Moreover it allocates an array of 1024 element even for this dummy case of two single pairs to storeā¦ anyway this does not seems to be the cause of the problem.
If I protect the hash table access with :
if (Playsets.Hashtableofvariables.Contains(1000))
{string st = Playsets.Hashtableofvariables[1000].ToString();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(st);
cdc.Write(bytes,0,bytes.Length);}
But I do not understand why I need such a precaution in this case. The playsets is declared as staticā¦ so GC should not delete it.
[EDIT]: I believed that the if would solve the issue but still have the exception while executing:
if (Playsets.Hashtableofvariables.Contains(1000))
This is strangeā¦ any idea on how to solve or isolate this problem?
@ Mike yes you may be true. I didnāt believe that locking was needed in this situation (a reading thread and a writing thread). I will try that. Do you know if thereās some high level mechanisms for such a lock? (mutexā¦ or something else?)
The problem is that I am not sure that this code is correct and will avoid deadlock or unsafe thread. Actually debugging my code step by step with breakpoints show me correct results while without breakpoints I have an Array list that contains duplicate elements which shouldnāt happened.
Are you sure that the T1 code is always used to add an entry to the hashtable?
One way to ensure this is to hide the hashtable from public access and provide an indexer property that implements code similar to your T1 and T2.