Is there a way to detect if an object is locked?

You can always call the static TryEnter method on the Monitor class using a value of 0 for the value to wait. If it is locked, then the call will return false.

However, the problem here is that you need to make sure that the list that you are trying to synchronize access to is being locked on itself in order to synchronize access.

It's generally bad practice to use the object that access is being synchronized as the object to lock on (exposing too much of the internal details of an object).

Remember, the lock could be on anything else, so just calling this on that list is pointless unless you are sure that list is what is being locked on.


Monitor.TryEnter will succeed if the object isn't locked, and will return false if at this very moment, the object is locked. However, note that there's an implicit race here: the instance this method returns, the object may not be locked any more.