Hello All,
I am having problem sharing information between two threads.
-- Thread 1 -- Reads value (calls ReadVal) from the Hashtable.
-- Thread 2 -- Adds value (calls Add) to the Hashtable.
What is happening is when i read the value, i get the exception :
Collection was modified; enumeration operation may not execute.
Any help would be greatly appreciated.
Thanks,
Murali Bala
I am having problem sharing information between two threads.
-- Thread 1 -- Reads value (calls ReadVal) from the Hashtable.
-- Thread 2 -- Adds value (calls Add) to the Hashtable.
What is happening is when i read the value, i get the exception :
Collection was modified; enumeration operation may not execute.
Any help would be greatly appreciated.
Code:
Class Hashes:
public static string ReadVal()
{
string str = string.Empty;
//lock (locker)
//{
IDictionaryEnumerator en = t.GetEnumerator();
//***Get the error here. ****
while (en.MoveNext())
{
str = en.Value.ToString();
t.Remove(en.Key);
}
//}
return str;
}
public static void Add(int val)
{
lock (locker)
{
try
{
t.Add("Hello:" + val, val);
}
catch (Exception)
{
int num = rc.Next();
t.Add("Hello:" + num, val);
}
}
}
Thanks,
Murali Bala