Rough context: messaging system, Communicator
sends messages (increases field _messagesInTransmission
), receives ACK or NAK on another thread (decreases _messagesInTransmission
).
If an instance of Communicator is disposed it should stop its underlying transport instance as soon as _messagesInTransmission
hits zero the next time.
How to implement this in a thread-safe way?
public void Dispose()
{
_state = State.Stopping; // immediately change state in order to prevent additional messages to be sent
// TODO: wait for _messagesInTransmission to become zero
}
Does the .NET framework support me with this task?
A simple solution would be a while(_mIT != 0) loop and some Thread.Sleep, however, I want to write more appealing code if possible.