Hello,
Why doesn't the lock in Class2::Execute protect the critical section:
public Class1
{
...
public DataTable SchemaTable = new DataTable();
}
Class1 creates many instances of Class2 and calls the corresponding
Execute function on different threads.
public Class2
{
...
public void Execute()
{
// sync multiple threads - ensure only one thread obtains the
schema table
// does not really work, many threads enter this section at the
same time.
lock (_parent.SchemaTable)
{
if (0 == _parent.SchemaTable.Count)
{
_parent.SchemaTable = rdr.GetSchemaTable(); // returns a new
DataTable
}
}
...
}
...
}
I think the problem is that rdr.GetSchemaTable returns a new DataTable
that _parent.SchemaTable points at but I am not really clear how the
..NET internals work. I am able to get around this problem by using a
dedicated SyncLock object.
I would appreciate if someone could point out clearly why the code
should not be expected to work.
Thanks