I have a chat connection class which once connected with the Connect method is able to send messages with the SendMessage method. Once the user is done they call Disconnect.
I want a lock which allows an unlimited number of SendMessage operations to take the lock, but only allows the Connect/Disconnect operatons to begin once they have an exclusive lock and all other SendMessage/Connect/Disconnect operations have exited and released the lock.
With this type of lock I could ensure the SendMessage has safe access to the internal Socket, SocketReader and SocketWriter classes and can be sure none of them will be disposed by a parallel Disconnect call.
It also has the benefit of queuing up all Connect/Disconnect, which obviously cannot run simultaneously.
Does such a lock exist in the .NET framework?
It seems you’re looking for
ReaderWriterLockSlim.