how to synchronize a hash table
Hello,
I wrote a test that calls the method EnumerateUsers simultaneously by N number of threads in a test app. (The actual object is exposed thru asp.net web services. UserManager is a singleton object in the webservice).
The problem I'm seeing is that when I'm seeing this.Add throw a dup-key exception because the key already exists in the hashtable.
I added instrumentation that showed synchronization at the "thisLock_" looks fine...threads are entering and exiting fine. However, i see that Add throw the exception, and the count of the hashtable is 1 (in the catch block) .
I must be doing this incorrect. Anyone have a tip? Thanks!
public class UserManager : Hashtable
private static Object thisLock_ = new Object();
public void EnumerateUsers()
{
lock (thisLock_)
{
DataReader dataReader ReadDatabase(sqlForUsers);
this.Clear();
while (dataReader.Read())
{
User user = new User(dataReader);
this.Add(user.Guid, user);
}
}

