Calling public classes from within a lock causing a DeadLock
public class ThreadedClass
{
private object syncHandle = new object();
public event EventHandler Updated = delegate { };
public int state = 0;
public void DoSmething()
{
lock(syncHandle)
{
// some locked code
state = 1;
Updated(this, EventArgs.Empty);
}
}
public int State {
get
{
int returnVal;
lock(syncHandle)
returnVal = state;
return returnVal;
}
}
}
You can't be certain what your client is going to call, Most likely they'll try to read the State property. Do this instead
public void DoSmething()
{
lock(syncHandle)
{
// some locked code
state = 1;
}
// this should be outside the lock
Updated(this, EventArgs.Empty);
}